Created
July 23, 2014 16:02
-
-
Save ortense/49db52b30709b8d08804 to your computer and use it in GitHub Desktop.
Node.js Router
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'), | |
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