Last active
December 11, 2015 09:08
-
-
Save nickleefly/4577405 to your computer and use it in GitHub Desktop.
server decode
This file contains hidden or 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'); | |
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