Skip to content

Instantly share code, notes, and snippets.

@nickleefly
Last active December 11, 2015 09:08
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 nickleefly/4577405 to your computer and use it in GitHub Desktop.
Save nickleefly/4577405 to your computer and use it in GitHub Desktop.
server decode
var http = require('http');
var url = require('url');
var pages = [
{id: '1', route: '', output: 'Woohoo!'},
{id: '2', route: 'about', output: 'A simple routing with Node example'},
{id: '3', route: 'another page', output: function() {return 'Here\'s '+this.route;}}
];
http.createServer(function (request, response) {
var id = url.parse(decodeURI(request.url), true).query.id;
pages.forEach(function(page) {
if (page.id === id) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.end(typeof page.output === 'function' ? page.output() : page.output);
}
});
if (!response.finished) {
response.writeHead(404);
response.end('Page Not Found!');
}
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment