Permalink
Browse files

Required querystring parameter name was being concatenated to last va…

…riable name in the route.
  • Loading branch information...
knowlecules committed Feb 20, 2013
1 parent f9fbb82 commit 0b490412348df49bc970e02d255e72a4c51cf8bd
Showing with 248 additions and 69 deletions.
  1. +183 −53 .idea/workspace.xml
  2. +29 −0 examples/client.js
  3. +19 −0 examples/server.js
  4. +1 −0 index.js
  5. +12 −12 lib/rapid-rest.js
  6. +4 −4 spec/routes-spec.js
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -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);
});
View
@@ -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'";
@@ -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);
View
@@ -0,0 +1 @@
+module.exports = require(__dirname + '/lib/rapid-rest');
View
@@ -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;
@@ -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) {
@@ -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) {
@@ -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();
@@ -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);
};
View
@@ -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\/([^/]*)\/([^/]*)/);

0 comments on commit 0b49041

Please sign in to comment.