Skip to content

Instantly share code, notes, and snippets.

@ortense
Created July 23, 2014 16:02
Show Gist options
  • Save ortense/49db52b30709b8d08804 to your computer and use it in GitHub Desktop.
Save ortense/49db52b30709b8d08804 to your computer and use it in GitHub Desktop.
Node.js Router
var http = require('http'),
fs = require('fs'),
htmlPath = './html',
server = http.createServer(function(req, res){
var pagina = req.url == '/' ? '/index' : req.url;
pagina = htmlPath + pagina + '.html';
fs.exists(pagina, function(exists) {
if (!exists) pagina = htmlPath + '/404.html';
fs.readFile(pagina, function(err, html){
if (err) {
throw err;
}
else {
res.writeHead(200, {"Content-Type": "text/html"});
res.write(html);
res.end();
}
});
});
});
server.listen(3000, function(){
console.log('Server Up');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment