Skip to content

Commit

Permalink
Required querystring parameter name was being concatenated to last va…
Browse files Browse the repository at this point in the history
…riable name in the route.
  • Loading branch information
knowlecules committed Feb 20, 2013
1 parent f9fbb82 commit 0b49041
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 69 deletions.
236 changes: 183 additions & 53 deletions .idea/workspace.xml

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions examples/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,35 @@ var req = http.request(options, function(res) {
});


req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});

// write data to request body
req.write(JSON.stringify(survey.answers));
req.end();

// calling jsonp
// http://127.0.0.1:8124/bw?table=words&level=11&callback=babyword=dojo.io.script.jsonp_dojoIoScript1._jsonpCallback
var collection = "there";
var survey = surveys[1];
options.path = '/bw?table=words&level=11&callback=babyword=dojo.io.script.jsonp_dojoIoScript1._jsonpCallback';

var req = http.request(options, function(res) {
if(res.statusCode != 201){
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
}else{
console.log('SUCCESS: ' + JSON.stringify(res.headers));
}
res.setEncoding('utf8');

res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});


req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
Expand Down
19 changes: 19 additions & 0 deletions examples/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ routes('/there/:over_name')
});
routes.listen(port);

/* Attempts to miss-match
params.level_name != params.level
*/
routes('/callback/:table_name?level_name={level_name}&callback={callback}')
('get', function(req, res, params, jsonData){
callbackWords(req, res, params, jsonData);
});

var interval;
function here(req, res, params, bodyContents) {
var msg = "got 'here'";
Expand All @@ -40,4 +48,15 @@ function there(req, res, params, bodyContents) {
res.writeHead(201,'' , {'Content-Type': 'text/html'});
res.end(msg);
};

function callbackWords(req, res, params, bodyContents) {
var msg = "went 'callbackwords'";
console.log(msg);
console.dir(bodyContents);
console.dir(params);

res.writeHead(201,'' , {'Content-Type': 'text/html'});
res.end(msg);
};

console.log(" listening on port:" + port);
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require(__dirname + '/lib/rapid-rest');
24 changes: 12 additions & 12 deletions lib/rapid-rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ var http = require('http');
var url = require('url');


module.exports = function() {
module.exports = function () {
/* Syntax to enable the following invocation signature :
routes('/my_path')('get', myFunc);
*/
var rest = function(route) {
var rest = function (route) {
return rest.handlers(rest.router.add(route));
};
rest.matchDefs = {};
rest.methods = {};
rest.respond = function(res, code, headers, data) {
rest.respond = function (res, code, headers, data) {
// Handles empty responses more gracefully for feedback purposes
if (code && headers && !data) {
data = headers;
Expand All @@ -43,17 +43,17 @@ module.exports = function() {
res.writeHead(code, headers);
res.end(data);
};
rest.handlers = function(route){
rest.handlers = function (route){
// Create options collection if it doesn't exist
route.methods.options = route.methods.options || function(req, res, params) {
var methods = Object.keys(route.methods).filter(function(key) {
route.methods.options = route.methods.options || function (req, res, params) {
var methods = Object.keys(route.methods).filter(function (key) {
return key !== 'options';
});
rest.respond(res, 200, methods);
};

// Request services are found at the intersection of service pattern and verb name
route.method = function(rawVerb, accept, fn) {
route.method = function (rawVerb, accept, fn) {

// Handle overloads
if (rawVerb && accept && !fn) {
Expand All @@ -64,12 +64,12 @@ module.exports = function() {
var verb = rawVerb.toLowerCase();
if (verb[0] !== 'p') {
// verbs that don't send data such as GET, SEARCH
route.methods[verb] = function(req, res, segments) {
route.methods[verb] = function (req, res, segments) {
fn(req, res, segments);
};
}else{
// Expecting POST or PUT verb
route.methods[verb] = function(req, res, segments) {
route.methods[verb] = function (req, res, segments) {
var body = '';
// Data accepting event
req.on('data', function (data) {
Expand Down Expand Up @@ -114,13 +114,13 @@ module.exports = function() {
var urlDef = url.parse(urlPath, true);
var urlMatch = urlDef.pathname.replace(/[\-\[\]\{\}\(\)\*\+\?\.,\/\\\^\$\|#\s]/g, "\\$&").replace(/(:[^(\\/)]+)/g,"([^/]*)");
var patternRE = new RegExp(urlMatch);
var paramKeys = (urlPath.indexOf("/:") > -1) ? urlPath.replace(/(\/[^:][^\/]+)/g, '').substr(2).split("/:") :[];
var paramKeys = (urlPath.indexOf("/:") > -1) ? urlDef.pathname.replace(/(\/[^:][^\/]+)/g, '').substr(2).split("/:") :[];
var queryKeys = urlDef.query;

rest.matchDefs[urlMatch] = rest.matchDefs[urlMatch] || {re:patternRE, paramKeys:paramKeys, queryKeys:queryKeys, methods:{}};
return rest.matchDefs[urlMatch];
},
handle : function(req, res) {
handle : function (req, res) {
var paramKeys, urlDef, pattern, key, ikey;
// Handle options
var method = req.method.toLowerCase();
Expand Down Expand Up @@ -178,7 +178,7 @@ module.exports = function() {
};
}());

rest.listen = function(host, port, cb) {
rest.listen = function (host, port, cb) {
http.createServer(rest.router.handle).listen(host, port, cb);
};

Expand Down
8 changes: 4 additions & 4 deletions spec/routes-spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var routes = require("../lib/rapid-rest")();

describe('rapid-rest-routes', function(){
describe('rapid-rest-routes', function () {

it('path with url params', function(){
it('path with url params', function () {
routes('/here/:here_name/:user')
('post', function(req, res, params, jsonData){
('post', function (req, res, params, jsonData) {
here(req, res, params, jsonData);
});
var rePattern = "\\/here\\/([^/]*)\\/([^/]*)";

var matchDef = routes.matchDefs[rePattern] ;
var matchDef = routes.matchDefs[rePattern];
expect(matchDef).not.toEqual(null);

expect(matchDef.re).toEqual(/\/here\/([^/]*)\/([^/]*)/);
Expand Down

0 comments on commit 0b49041

Please sign in to comment.