Permalink
Browse files

added node.js interface

  • Loading branch information...
joshbuddy committed Jan 18, 2010
1 parent 4b6dff4 commit f252d9c39b75d65eb002b76a7f1f8111b9cfc7e8
Showing with 44 additions and 1 deletion.
  1. +15 −0 exmaples/node-example.js
  2. +29 −1 lib/sherpa.js
View
@@ -0,0 +1,15 @@
+require('../lib/sherpa')
+
+var sys = require("sys"), http = require("http");
+var sherpa = new Sherpa.interfaces.NodeJs({
+ '/hello': {to: function (request, response) {
+ response.sendHeader(200, {"Content-Type": "text/plain"});
+ response.sendBody("Hello World\n");
+ response.finish();
+ }}
+});
+
+http.createServer(sherpa.listener()).listen(8000);
+sys.puts("Server running at http://127.0.0.1:8000/");
+
+
View
@@ -29,7 +29,8 @@ Sherpa = {
Route: function(router, finalNode) {
this.router = router;
this.finalNode = finalNode;
- }
+ },
+ interfaces: { }
}
Sherpa.Router.prototype = {
@@ -307,3 +308,30 @@ Sherpa.Node.prototype = {
return this.lookup[part];
}
};
+
+Sherpa.interfaces.NodeJs = function(routes) {
+ this.routes = routes;
+}
+
+Sherpa.interfaces.NodeJs.prototype = {
+ listener: function() {
+ var router = new Sherpa.Router();
+ 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);
+ }
+
+ return function(httpRequest, httpResponse) {
+ var response = router.recognize(httpRequest.uri.path, httpRequest);
+ if (response) {
+ httpRequest.sherpa_response = response;
+ response.destination.call(undefined, httpRequest, httpResponse);
+ } else {
+ httpResponse.sendHeader(404, {});
+ httpResponse.finish();
+ }
+ }
+
+ }
+}

0 comments on commit f252d9c

Please sign in to comment.