Here's my code for a consise node.js HTTP server. It does work as intended, but I'd like to ask if there's any glitch or to be improved.
var httpServer = function(dir)
{
var component = require('http')
.createServer(function(req, res)
{
var fs = require('fs');
var path = require("path");
var url = require('url');
var mimeTypes = {
"html": "text/html",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"png": "image/png",
"js": "text/javascript",
"css": "text/css"
};
var uri = url.parse(req.url)
.pathname;
var filename = path.join(dir, unescape(uri));
var indexFilename = path.join(dir, unescape('index.html'));
var stats;
console.log(filename);
try
{
stats = fs.lstatSync(filename); // throws if path doesn't exist
}
catch (e)
{
res.writeHead(404,
{
'Content-Type': 'text/plain'
});
res.write('404 Not Found\n');
res.end();
return;
}
if (stats.isFile())
{
// path exists, is a file
var mimeType = mimeTypes[path.extname(filename)
.split(".")[1]];
res.writeHead(200,
{
'Content-Type': mimeType
});
var fileStream =
fs.createReadStream(filename)
.pipe(res);
}
else if (stats.isDirectory())
{
// path exists, is a directory
res.writeHead(200,
{
'Content-Type': "text/html"
});
var fileStream =
fs.createReadStream(indexFilename)
.pipe(res);
}
else
{
// Symbolic link, other?
// TODO: follow symlinks? security?
res.writeHead(500,
{
'Content-Type': 'text/plain'
});
res.write('500 Internal server error\n');
res.end();
}
});
return component;
};
var port = 9999;
var dir = 'www';
var HTTPserver =
httpServer(require('path')
.join(__dirname, dir))
.listen(port, function()
{
console.log('HTTP listening ' + port);
});