Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
a small example on how to handle routing, not meant for production use
var http = require('http'), //http server
urlparser = require('url'), //url parser & builder
host = 'sandbox',
port = 1337;
http.createServer(function (req, res) {
var response = 'Hello, World!\n',
url = urlparser.parse(req.url, true).pathname;
switch (url) {
case('/hello'): //check for path, alternatively strip the front slash
response = 'World!\n';
break;
case('/world'): //check for path, alternatively strip the front slash
response = 'Hello, \n';
break;
}
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(response);
}).listen(port, host);
console.log('Server running at http://' + host + ':' + port + '/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment