Permalink
Browse files

nicer node example and 'not found' support

  • Loading branch information...
1 parent f252d9c commit 7f0ce22a4c5b19e76f11c780df623a6709df9987 @joshbuddy committed Jan 18, 2010
Showing with 23 additions and 3 deletions.
  1. +11 −0 exmaples/node-example.js
  2. +12 −3 lib/sherpa.js
View
11 exmaples/node-example.js
@@ -6,7 +6,18 @@ var sherpa = new Sherpa.interfaces.NodeJs({
response.sendHeader(200, {"Content-Type": "text/plain"});
response.sendBody("Hello World\n");
response.finish();
+ }},
+ '/hello/:name': {to: function (request, response) {
+ response.sendHeader(200, {"Content-Type": "text/plain"});
+ response.sendBody("Hello "+request.sherpa_response.params['name']+"\n");
+ response.finish();
+ }},
+ 'not found': {to: function (request, response) {
+ response.sendHeader(404, {"Content-Type": "text/plain"});
+ response.sendBody("I can't find what you're looking for..");
+ response.finish();
}}
+
});
http.createServer(sherpa.listener()).listen(8000);
View
15 lib/sherpa.js
@@ -316,10 +316,20 @@ Sherpa.interfaces.NodeJs = function(routes) {
Sherpa.interfaces.NodeJs.prototype = {
listener: function() {
var router = new Sherpa.Router();
+ var notFound = function(req, res) {
+ res.sendHeader(404, {});
+ res.finish();
+ }
+
+
for(var key in this.routes) {
var destination = this.routes[key].to;
delete this.routes[key]['to'];
- var route = router.add(key, this.routes[key]).to(destination);
+ if (key == 'not found') {
+ notFound = destination;
+ } else {
+ router.add(key, this.routes[key]).to(destination);
+ }
}
return function(httpRequest, httpResponse) {
@@ -328,8 +338,7 @@ Sherpa.interfaces.NodeJs.prototype = {
httpRequest.sherpa_response = response;
response.destination.call(undefined, httpRequest, httpResponse);
} else {
- httpResponse.sendHeader(404, {});
- httpResponse.finish();
+ notFound.call(undefined, httpRequest, httpResponse);
}
}

0 comments on commit 7f0ce22

Please sign in to comment.