Permalink
Browse files

Merge branch 'master' of github.com:jdub/node-router

  • Loading branch information...
2 parents 5d70b7a + 64834cf commit c7c669e5911561fa4d071931698788d7e586f2d4 @jdub jdub committed Aug 24, 2010
Showing with 28 additions and 12 deletions.
  1. +28 −12 lib/node-router.js
View
@@ -36,7 +36,8 @@ function notFound(req, res, message) {
"Content-Type": "text/plain",
"Content-Length": message.length
});
- res.write(message);
+ if (req.method !== "HEAD")
+ res.write(message);
res.end();
}
@@ -95,6 +96,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) {
@@ -181,7 +185,8 @@ exports.getServer = function getServer(logger) {
[ ["Content-Type", content_type],
["Content-Length", Buffer.byteLength(body, 'utf8')]
]));
- res.write(body, 'utf8');
+ if (req.method !== "HEAD")
+ res.write(body, 'utf8');
res.end();
}
@@ -200,7 +205,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;
@@ -218,7 +229,7 @@ exports.getServer = function getServer(logger) {
match.unshift(req);
if (route.format !== undefined) {
var body = "";
- req.setBodyEncoding('utf8');
+ req.setEncoding('utf8');
req.addListener('data', function (chunk) {
body += chunk;
});
@@ -249,24 +260,27 @@ exports.getServer = function getServer(logger) {
}
}
}
-
+
notFound(req, res);
}
doRoute();
});
+
function listen(port, host) {
port = port || 8080;
- if (typeof port === 'number') {
- host = host || "127.0.0.1";
- }
+
+ if (typeof host === 'undefined' || host == '*')
+ host = null ;
+
+
server.listen(port, host);
if (typeof port === 'number') {
logger("node-router server instance at http://" + host + ":" + port + "/");
} else {
logger("node-router server instance at unix:" + port);
- }
+ }
}
function end() {
@@ -283,7 +297,7 @@ exports.getServer = function getServer(logger) {
resource: resource,
resourceController: resourceController,
listen: listen,
- end: end,
+ end: end
};
}
@@ -319,7 +333,8 @@ exports.staticHandler = function (filename) {
return function (req, res) {
loadResponseData(req, res, function () {
res.writeHead(200, headers);
- res.write(body, encoding);
+ if (req.method !== "HEAD")
+ res.write(body, encoding);
res.end();
});
};
@@ -352,7 +367,8 @@ exports.staticDirHandler = function(root, prefix) {
if (filename == root) filename = path.join(root, 'index.html');
loadResponseData(req, res, filename, function(headers, body, encoding) {
res.writeHead(200, headers);
- res.write(body, encoding);
+ if (req.method !== "HEAD")
+ res.write(body, encoding);
res.end();
});
};

0 comments on commit c7c669e

Please sign in to comment.