Skip to content

Commit

Permalink
Added logging for errors (e.g. file not found)
Browse files Browse the repository at this point in the history
  • Loading branch information
Elad Moshe authored and indexzero committed Mar 19, 2015
1 parent 630900f commit f328b20
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
11 changes: 8 additions & 3 deletions bin/http-server
Expand Up @@ -43,9 +43,14 @@ var port = argv.p || parseInt(process.env.PORT, 10),
requestLogger;

if (!argv.s && !argv.silent) {
requestLogger = function(req) {
log('[%s] "%s %s" "%s"', (new Date).toUTCString(), req.method.cyan, req.url.cyan, req.headers['user-agent']);
}
requestLogger = function(req, res, error) {
var date = (new Date).toUTCString();
if (error) {
log('[%s] "%s %s" Error (%s): "%s"', date, req.method.red, req.url.red, error.status.toString().red, error.message.red);
} else {
log('[%s] "%s %s" "%s"', date, req.method.cyan, req.url.cyan, req.headers['user-agent']);
}
};
}

if (!port) {
Expand Down
8 changes: 7 additions & 1 deletion lib/http-server.js
Expand Up @@ -71,7 +71,13 @@ var HTTPServer = exports.HTTPServer = function (options) {

var serverOptions = {
before: before,
headers: this.headers
headers: this.headers,
onError: function (err, req, res) {
if (options.logFn) {
options.logFn(req, res, err);
}
res.end();
}
};

if (options.https) {
Expand Down

0 comments on commit f328b20

Please sign in to comment.