Paste: Simple Web Server
Author: | Rex |
Mode: | javascript |
Date: | Tue, 19 Aug 2014 05:44:37 |
Plain Text |
var http = require('http');
var fs = require('fs');
var jade = require('jade');
var mimeTypes = {
"js" : "text/javascript",
"json" : "text/json",
"html" : "text/html",
"css" : "text/css",
"gif" : "image/gif",
"png" : "image/png",
"txt" : "text/plain",
"xhtml" : "application/xhtml",
"ico" : "image/png",
"appcache": "text/cache-manifest",
"jade" : "text/html",
"wav" : "audio/wav",
"svgz" : "image/svg+xml",
"svg" : "image/svg+xml",
"jpg" : "image/jpeg"
}
function getExtension(filename) {
var i = filename.lastIndexOf('.')
return (i < 0) ? '' : filename.substr(i + 1)
}
http.createServer(function (req, res) {
var path = req.url.substring(1);
var mimeType = mimeTypes[getExtension(path)]
if(fs.existsSync(path)){
res.writeHead(200, {'Content-Type': mimeType});
var ext = getExtension(path);
console.log(path);
if(ext == "jade"){
var fn = jade.compileFile(path);
res.end(fn());
}else{
var txt = fs.readFileSync(path).toString();
res.end(txt);
}
}else{
console.log("404: "+path);
res.writeHead(404, {'Content-Type':'text/plain' });
res.end("File not found!");
}
}).listen(80,'127.0.0.1');
console.log('Simple Server Running');
New Annotation