Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

Conflicts:
	lib/node-router.js
  • Loading branch information...
commit fc5f66c5ce0a0313c236185e1d344ac42dc1f805 2 parents e813d1d + 0d69329
@creationix authored
Showing with 28 additions and 21 deletions.
  1. +28 −21 lib/node-router.js
View
49 lib/node-router.js
@@ -47,7 +47,7 @@ function logify(req, res, logger) {
var end = res.end;
res.end = function () {
// Common Log Format (mostly)
- logger((req.socket && req.socket.remoteAddress) + " - - [" + (new Date()).toUTCString() + "]"
+ logger((req.socket && req.socket.remoteAddress) + " - - [" + (new Date()).toUTCString() + "]"
+ " \"" + req.method + " " + req.url
+ " HTTP/" + req.httpVersionMajor + "." + req.httpVersionMinor + "\" "
+ res.statusCode + " - \""
@@ -64,9 +64,9 @@ function logify(req, res, logger) {
exports.getServer = function getServer(logger) {
logger = logger || sys.puts;
-
+
var routes = [];
-
+
// Adds a route the the current server
function addRoute(method, pattern, handler, format) {
if (typeof pattern === 'string') {
@@ -108,7 +108,7 @@ exports.getServer = function getServer(logger) {
put(new RegExp('^/' + name + '/([^/]+)$'), controller.update, format);
del(new RegExp('^/' + name + '/([^/]+)$'), controller.destroy);
};
-
+
function resourceController(name, data, on_change) {
data = data || [];
on_change = on_change || function () {};
@@ -152,13 +152,13 @@ exports.getServer = function getServer(logger) {
});
},
destroy: function (req, res, id) {
- delete(data[id]);
+ delete data[id];
on_change(id);
res.simpleJson(200, "200 Destroyed");
}
};
};
-
+
// Create the http server object
var server = http.createServer(function (req, res) {
@@ -205,17 +205,17 @@ 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;
-
+
for (var i = 0, l = routes.length; i < l; i += 1) {
var route = routes[i];
if (req.method === route.method) {
@@ -229,13 +229,17 @@ 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;
});
req.addListener('end', function () {
if (route.format === 'json') {
- body = JSON.parse(unescape(body));
+ try {
+ body = JSON.parse(unescape(body));
+ } catch(e) {
+ body = null;
+ }
}
match.push(body);
route.handler.apply(null, match);
@@ -251,7 +255,7 @@ exports.getServer = function getServer(logger) {
res.simpleJson(200, result);
break;
}
-
+
return;
}
}
@@ -263,23 +267,26 @@ exports.getServer = function getServer(logger) {
});
- function listen(port, host) {
+
+ function listen(port, host, callback) {
port = port || 8080;
+
+ if (typeof host === 'undefined' || host == '*')
+ host = null;
+
+ server.listen(port, host, callback);
+
if (typeof port === 'number') {
- host = host || "127.0.0.1";
- }
- server.listen(port, host);
- if (typeof port === 'number') {
- logger("node-router server instance at http://" + host + ":" + port + "/");
+ logger("node-router server instance at http://" + (host || '*') + ":" + port + "/");
} else {
logger("node-router server instance at unix:" + port);
}
}
-
+
function end() {
return server.end();
}
-
+
// Return a handle to the public facing functions from this closure as the
// server object.
return {
@@ -290,7 +297,7 @@ exports.getServer = function getServer(logger) {
resource: resource,
resourceController: resourceController,
listen: listen,
- end: end,
+ end: end
};
}
Please sign in to comment.
Something went wrong with that request. Please try again.