patternjavascriptMinor
Concise HTTP server
Viewed 0 times
serverhttpconcise
Problem
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 s
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 s
Solution
You should move all
In its current form, you won't see
Rule of Repair (TAOUP): Repair what you can — but when you must fail, fail noisily and as soon as possible.
require(...) calls to the very beginning of your code, instead of running require on each place locally.In its current form, you won't see
require errors until your server runs and receives its first requests. This is unfortunate. You usually want a program to fail as early as possible if it clearly won't work.Rule of Repair (TAOUP): Repair what you can — but when you must fail, fail noisily and as soon as possible.
Context
StackExchange Code Review Q#78857, answer score: 3
Revisions (0)
No revisions yet.