Skip to content

Instantly share code, notes, and snippets.

@avalanche123
Created September 29, 2010 02:14
Show Gist options
  • Save avalanche123/602196 to your computer and use it in GitHub Desktop.
Save avalanche123/602196 to your computer and use it in GitHub Desktop.
// http package is used to create http servers
var http = require('http');
// create server that will write string 'Hello World' with a new line on every request to ip 127.0.0.1, port 8124
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
// Log message after createServer is successfully sent
console.log('Server running at //127.0.0.1:8124/');
// we will use sys.log for logging
var sys = require('sys');
// send open file command, with callback
fs.open('path/to/file.js', 'w', 0777, function(err, fd) {
//throw error, if there was one
if (err) throw err;
// send write command with opened file, content, starting point, encoding and callback
fs.write(fd, 'Hello World!', 0, 'utf8', function (err, written) {
//throw error, if there was one
if (err) throw err;
// after successful file write, log number of bytes written
sys.log('Success! Wrote ' + written + ' bytes');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment