Permalink
Browse files

Allow simple sync responses for html (strings) and json (objects).

  • Loading branch information...
1 parent 55dbed2 commit 313b036af6febb777e14d8a6efb40cb4490fd3b4 @creationix committed Mar 26, 2010
Showing with 17 additions and 3 deletions.
  1. +6 −1 hello_world.js
  2. +10 −1 lib/node-router.js
  3. +1 −1 package.json
View
@@ -1,7 +1,12 @@
var server = require('./lib/node-router').getServer();
+server.get("/json", function (req, res, match) {
+ return {hello: "World"};
+});
+
server.get(new RegExp("^/(.*)$"), function hello(req, res, match) {
- res.simpleHtml(200, "Hello " + (match || "World") + "!");
+ return "Hello " + (match || "World") + "!";
});
+
server.listen(8080);
View
@@ -203,7 +203,16 @@ exports.getServer = function getServer(logger) {
});
return;
}
- route.handler.apply(null, match);
+ var result = route.handler.apply(null, match);
+ switch (typeof result) {
+ case "string":
+ res.simpleHtml(200, result);
+ break;
+ case "object":
+ res.simpleJson(200, result);
+ break;
+ }
+
return;
}
}
View
@@ -1,5 +1,5 @@
{ "name" : "node-router"
-, "version" : "0.0.1"
+, "version" : "0.0.2"
, "description" : "A simple http server for node.js that has sinatra like qualities. Ideal for generating web services via node."
, "author" : "Tim Caswell <tim@creationix.org>"
, "main" : "lib/node-router"

0 comments on commit 313b036

Please sign in to comment.