Skip to content

Commit

Permalink
Add support for HEAD http requests and do not let crash applications
Browse files Browse the repository at this point in the history
  • Loading branch information
djui authored and jdub committed Aug 24, 2010
1 parent fe4824e commit 8f69ffa
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/node-router.js
Expand Up @@ -95,6 +95,9 @@ exports.getServer = function getServer(logger) {
function del(pattern, handler) {
return addRoute("DELETE", pattern, handler);
}
function head(pattern, handler) {
return addRoute("HEAD", pattern, handler);
}

// This is a meta pattern that expands to a common RESTful mapping
function resource(name, controller, format) {
Expand Down Expand Up @@ -200,7 +203,13 @@ exports.getServer = function getServer(logger) {
res.notFound = function (message) {
notFound(req, res, message);
};


res.onlyHead = function (code, extra_headers) {
res.writeHead(code, (extra_headers || []).concat(
[["Content-Type", content_type]]));
res.end();
}

function doRoute() {
uri = url_parse(req.url);
path = uri.pathname;
Expand Down Expand Up @@ -245,8 +254,11 @@ exports.getServer = function getServer(logger) {
}
}
}

notFound(req, res);

if (req.method === "HEAD")
res.onlyHead(200);
else
notFound(req, res);
}
doRoute();

Expand All @@ -262,7 +274,7 @@ exports.getServer = function getServer(logger) {
logger("node-router server instance at http://" + host + ":" + port + "/");
} else {
logger("node-router server instance at unix:" + port);
}
}
}

function end() {
Expand Down

0 comments on commit 8f69ffa

Please sign in to comment.