Created
May 22, 2017 00:18
-
-
Save alexpilugin/31daf713f0f083f99e0aac6458b877f7 to your computer and use it in GitHub Desktop.
OReilly. Ethan Brown. Web Development With Node And Express
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.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