Being new to Node.js
, I'd like to know of a better way of implementing this:
server = http.createServer( function(req, res) {
if (req.url === '/') {
fs.readFile('index.html', function(err, page) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(page);
res.end();
});
}
else if (req.url == '/new.html') {
fs.readFile('new.html', function(err, page) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(page);
res.end();
});
}
});
server.listen(8080);
Using something like this:
var Url = require('url')
...
// In request handler
var uri = Url.parse(req.url);
switch (uri.pathname) {
case "/":
..
case "/new.html":
..
}