Skip to content

Instantly share code, notes, and snippets.

@paddybyers
Created November 17, 2011 16:13
Show Gist options
  • Save paddybyers/1373549 to your computer and use it in GitHub Desktop.
Save paddybyers/1373549 to your computer and use it in GitHub Desktop.
Simple "hello world" http server for node.js
console.log(process.argv[0]);
console.log(process.argv[1]);
console.log(process.argv[2]);
var port = process.argv.length > 2 ? Number(process.argv[2]) : 1337;
var http = require('http');
var svr = http.createServer(function (req, res) {
if(req.url.indexOf('exit') != -1) {
res.write('exiting');
res.end();
svr.close();
return;
//process.exit(0);
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<h1>Hello World<\h1>');
res.write('<a href="exit">exit</a>');
res.end();
}).listen(port, "127.0.0.1");
console.log('Server running at http://127.0.0.1:' + port + '/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment