Skip to content

Instantly share code, notes, and snippets.

@alexpilugin
Created May 22, 2017 00:18
Show Gist options
  • Save alexpilugin/31daf713f0f083f99e0aac6458b877f7 to your computer and use it in GitHub Desktop.
Save alexpilugin/31daf713f0f083f99e0aac6458b877f7 to your computer and use it in GitHub Desktop.
OReilly. Ethan Brown. Web Development With Node And Express
var http = require('http');
http.createServer(function (req, res) {
// normalize url by removing querystring, optional
// trailing slash, and making it lowercase
var path = req.url.replace(/\/?(?:\?.*)?$/, '').toLowerCase();
switch (path) {
case '':
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>Homepage</h1>');
break;
case '/about':
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>About</h1>');
break;
default:
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<h1>Not Found</h1>');
break;
}
}).listen(3000);
console.log('Server started on localhost:3000; press Ctrl-C to terminate....');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment