ortense
7/23/2014 - 4:02 PM

Node.js Router

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');
});