Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixed node example

  • Loading branch information...
commit d44317e7b4bdd6dcae2ea0c8f7eaf760e4ea29ec 1 parent 01da3eb
@joshbuddy authored
View
36 exmaples/node-example.js
@@ -1,32 +1,32 @@
-require('../lib/sherpa')
+require('../lib/sherpa/nodejs')
var sys = require("sys"), http = require("http");
-http.createServer(new Sherpa.interfaces.NodeJs([
+http.createServer(new Sherpa.NodeJs([
['/hello', function (request, response) {
- response.sendHeader(200, {"Content-Type": "text/plain"});
- response.sendBody("Hello World\n");
- response.finish();
+ response.writeHead(200, {"Content-Type": "text/plain"});
+ response.write("Hello World\n");
+ response.close();
}],
- ['/hello/:name', {matchesWith: {name: /^\d+$/}}, function (request, response) {
- response.sendHeader(200, {"Content-Type": "text/plain"});
- response.sendBody("Hello Number "+request.sherpaResponse.params['name']+"\n");
- response.finish();
+ ['/hello/:name', {matchesWith: {name: /\d+/}}, function (request, response) {
+ response.writeHead(200, {"Content-Type": "text/plain"});
+ response.write("Hello Number "+request.sherpaResponse.params['name']+"\n");
+ response.close();
}],
['/hello/:name', function (request, response) {
- response.sendHeader(200, {"Content-Type": "text/plain"});
- response.sendBody("Hello "+request.sherpaResponse.params['name']+"\n");
- response.finish();
+ response.writeHead(200, {"Content-Type": "text/plain"});
+ response.write("Hello "+request.sherpaResponse.params['name']+"\n");
+ response.close();
}],
['/hello/:name', {conditions:{method: 'POST'}}, function (request, response) {
- response.sendHeader(200, {"Content-Type": "text/plain"});
- response.sendBody("Hello POSTY "+request.sherpaResponse.params['name']+"\n");
- response.finish();
+ response.writeHead(200, {"Content-Type": "text/plain"});
+ response.write("Hello POSTY "+request.sherpaResponse.params['name']+"\n");
+ response.close();
}],
['not found', function (request, response) {
- response.sendHeader(404, {"Content-Type": "text/plain"});
- response.sendBody("I can't find what you're looking for..");
- response.finish();
+ response.writeHead(404, {"Content-Type": "text/plain"});
+ response.write("I can't find what you're looking for..");
+ response.close();
}]
]).listener()).listen(8000);
View
25 lib/sherpa.js
@@ -162,10 +162,11 @@ Sherpa.Node.prototype = {
var lin = this.linear[linearIdx];
var match = lin[0].exec(wholePath);
if (match) {
+ var newParams = params.concat(match.slice(0, lin[1]));
matchedIndex = match.shift().length;
var resplitParts = wholePath.substring(matchedIndex).split('/');
if (resplitParts.length == 1 && resplitParts[0] == '') resplitParts.shift();
- var potentialMatch = lin[2].find(resplitParts, request, params.concat(match));
+ var potentialMatch = lin[2].find(resplitParts, request, newParams);
if (potentialMatch) return potentialMatch;
}
}
@@ -287,12 +288,27 @@ Sherpa.Router.prototype = {
};
Sherpa.Route.prototype = {
+ withOptions: function(options) {
+ if (options['conditions']) {
+ this.condition(options['conditions']);
+ }
+ if (options['matchesWith']) {
+ this.matchesWith(options['matchesWith']);
+ }
+ if (options['matchPartially']) {
+ this.matchPartially(options['matchPartially']);
+ }
+ if (options['name']) {
+ this.matchPartially(options['name']);
+ }
+ return this;
+ },
name: function(routeName) {
this.router.routes[routeName] = this;
return this;
},
- matchPartially: function() {
- this.partial = true;
+ matchPartially: function(partial) {
+ this.partial = (partial === undefined || partial === true);
return this;
},
matchesWith: function(matches) {
@@ -403,6 +419,3 @@ Sherpa.Path.prototype = {
}
}
};
-
-Sherpa.Response.prototype = {
-};
View
0  lib/sherpa/interfaces/Jsgi.js → lib/sherpa/Jsgi.js
File renamed without changes
View
12 lib/sherpa/interfaces/NodeJs.js → lib/sherpa/nodejs.js
@@ -1,12 +1,14 @@
-exports.NodeJs = NodeJs;
+require('../sherpa');
+
+exports.Sherpa = Sherpa;
var url = require('url');
-function NodeJs(routes) {
+Sherpa.NodeJs = function (routes) {
this.routes = routes;
-}
+};
-NodeJs.prototype = {
+Sherpa.NodeJs.prototype = {
listener: function() {
var router = new Sherpa.Router();
var notFound = function(req, res) {
@@ -23,7 +25,7 @@ NodeJs.prototype = {
router.add(this.routes[key][0]).to(this.routes[key][1]);
break;
case 3:
- router.add(this.routes[key][0], this.routes[key][1]).to(this.routes[key][2]);
+ router.add(this.routes[key][0]).withOptions(this.routes[key][1]).to(this.routes[key][2]);
break;
default:
throw("must be 2 or 3");
Please sign in to comment.
Something went wrong with that request. Please try again.