Created
April 13, 2017 09:14
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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