Skip to content

Instantly share code, notes, and snippets.

@kumarldh
Created April 13, 2017 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kumarldh/80a4c78617c2e3cd49defd07286105a3 to your computer and use it in GitHub Desktop.
Save kumarldh/80a4c78617c2e3cd49defd07286105a3 to your computer and use it in GitHub Desktop.
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