Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
+16
-12
@@ -7,12 +7,12 @@ var url = require('url'); | ||
| * | ||
| * flattenKeys({'a': {'b': function(){}, 'c': function(){}}}) | ||
| * {'ab': function(){}, 'ac': function(){}} | ||
| * flattenKeys({'a': {'b': function () {}, 'c': function () {}}}) | ||
| * {'ab': function () {}, 'ac': function () {}} | ||
| * | ||
| */ | ||
| function flattenKeys(obj, /*optional args: */acc, prefix, prev_method){ | ||
| function flattenKeys(obj, /*optional args: */acc, prefix, prev_method) { | ||
| acc = acc || []; | ||
| prefix = prefix || ''; | ||
| Object.keys(obj).forEach(function(k){ | ||
| Object.keys(obj).forEach(function (k) { | ||
| var split = splitURL(k); | ||
| if(typeof obj[k] == 'function') { | ||
| if (typeof obj[k] == 'function') { | ||
| acc.push([prefix + split.url, split.method || prev_method, obj[k]]) | ||
@@ -31,7 +31,7 @@ } | ||
| * | ||
| * compileKeys([['abc', 'GET', function(){}], ['xyz', 'POST', function(){}]]) | ||
| * [[/^abc$/, 'GET', function(){}], [/^xyz$/, 'POST', function(){}]] | ||
| * compileKeys([['abc', 'GET', function () {}], ['xyz', 'POST', function () {}]]) | ||
| * [[/^abc$/, 'GET', function () {}], [/^xyz$/, 'POST', function () {}]] | ||
| */ | ||
| function compileKeys(urls){ | ||
| return urls.map(function(url){ | ||
| function compileKeys(urls) { | ||
| return urls.map(function (url) { | ||
| // replace named params with regexp groups | ||
@@ -62,5 +62,9 @@ var pattern = url[0].replace(/\/:\w+/g, '(?:/([^\/]+))'); | ||
| */ | ||
| module.exports = function(urls){ | ||
| module.exports = function (urls) { | ||
| var compiled = compileKeys(flattenKeys(urls)); | ||
| return function(req, res, next){ | ||
| return function (req, res, next) { | ||
| var args = [req, res]; | ||
| if (next) { | ||
| args.push(next); | ||
| } | ||
| if(!compiled.some(function(x){ | ||
@@ -70,3 +74,3 @@ var match = x[0].exec(url.parse(req.url).pathname); | ||
| if (!x[1] || x[1] === req.method) { | ||
| x[2].apply(null, [req, res, next].concat(match.slice(1))); | ||
| x[2].apply(null, args.concat(match.slice(1))); | ||
| return true; | ||
@@ -73,0 +77,0 @@ } |
+24
-14
@@ -1,16 +0,26 @@ | ||
| { "name": "dispatch" | ||
| , "description": "A regular expression URL dispatcher for Connect" | ||
| , "main": "./index" | ||
| , "author": "Caolan McMahon" | ||
| , "version": "0.2.0" | ||
| , "repository" : | ||
| { "type" : "git" | ||
| , "url" : "http://github.com/caolan/dispatch.git" | ||
| { | ||
| "name": "dispatch", | ||
| "description": "A regular expression URL dispatcher for Connect", | ||
| "main": "./lib/dispatch", | ||
| "author": "Caolan McMahon", | ||
| "version": "1.0.0", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "http://github.com/caolan/dispatch.git" | ||
| }, | ||
| "bugs": { | ||
| "url": "http://github.com/caolan/dispatch/issues" | ||
| }, | ||
| "licenses": [ | ||
| { | ||
| "type": "MIT", | ||
| "url": "http://github.com/caolan/dispatch/raw/master/LICENSE" | ||
| } | ||
| ], | ||
| "scripts": { | ||
| "test": "./node_modules/.bin/nodeunit test" | ||
| }, | ||
| "devDependencies": { | ||
| "nodeunit": "^0.9.0" | ||
| } | ||
| , "bugs" : { "url" : "http://github.com/caolan/dispatch/issues" } | ||
| , "licenses" : | ||
| [ { "type" : "MIT" | ||
| , "url" : "http://github.com/caolan/dispatch/raw/master/LICENSE" | ||
| } | ||
| ] | ||
| } |
+100
-68
| # Dispatch | ||
| A really simple URL dispatcher for | ||
| [Connect](http://github.com/senchalabs/connect). Allows arbitrarily nested | ||
| regular expressions for matching URLs and calling an associated function. | ||
| [Connect](http://github.com/senchalabs/connect) or a plain Node.js HTTP Server. | ||
| Allows arbitrarily nested regular expressions for matching URLs and calling an | ||
| associated function. | ||
| var Connect = require('connect'), | ||
| dispatch = require('dispatch'); | ||
| ```js | ||
| var Connect = require('connect'), | ||
| dispatch = require('dispatch'); | ||
| Connect.createServer( | ||
| dispatch({ | ||
| '/about': function(req, res, next){ | ||
| ... | ||
| }, | ||
| '/user/:id': function(req, res, next, id){ | ||
| ... | ||
| }, | ||
| '/user/posts': function(req, res, next){ | ||
| ... | ||
| }, | ||
| '/user/posts/(\\w+)': function(req, res, next, post){ | ||
| ... | ||
| } | ||
| }) | ||
| ); | ||
| Connect.createServer( | ||
| dispatch({ | ||
| '/about': function(req, res, next){ | ||
| ... | ||
| }, | ||
| '/user/:id': function(req, res, next, id){ | ||
| ... | ||
| }, | ||
| '/user/posts': function(req, res, next){ | ||
| ... | ||
| }, | ||
| '/user/posts/(\\w+)': function(req, res, next, post){ | ||
| ... | ||
| } | ||
| }) | ||
| ); | ||
| ``` | ||
| Or, using a vanilla HTTP Server: | ||
| ```js | ||
| var http = require('http'); | ||
| var server = http.createServer( | ||
| dispatch({ | ||
| '/about': function(req, res){ | ||
| ... | ||
| }, | ||
| '/user/:id': function(req, res, id){ | ||
| ... | ||
| } | ||
| }) | ||
| ); | ||
| server.listen(8080); | ||
| ``` | ||
| Dispatch can be used with a straight-forward object literal containing view | ||
@@ -38,12 +60,14 @@ functions keyed by URL. As you can see from the last URL in the list, captured | ||
| Connect.createServer( | ||
| dispatch({ | ||
| '/about': function(req, res, next){ ... }, | ||
| '/user': { | ||
| '/': function(req, res, next){ ... }, | ||
| '/posts': function(req, res, next){ ... }, | ||
| '/posts/(\\w+)': function(req, res, next, post){ ... } | ||
| } | ||
| }) | ||
| ); | ||
| ```js | ||
| Connect.createServer( | ||
| dispatch({ | ||
| '/about': function(req, res, next){ ... }, | ||
| '/user': { | ||
| '/': function(req, res, next){ ... }, | ||
| '/posts': function(req, res, next){ ... }, | ||
| '/posts/(\\w+)': function(req, res, next, post){ ... } | ||
| } | ||
| }) | ||
| ); | ||
| ``` | ||
@@ -56,8 +80,10 @@ This helps you tidy up the structure to make it more readable. It also makes | ||
| Connect.createServer( | ||
| dispatch({ | ||
| '/about': function(req, res, next){ ... }, | ||
| '/user': require('./user').urls | ||
| }) | ||
| ); | ||
| ```js | ||
| Connect.createServer( | ||
| dispatch({ | ||
| '/about': function(req, res, next){ ... }, | ||
| '/user': require('./user').urls | ||
| }) | ||
| ); | ||
| ``` | ||
@@ -69,10 +95,12 @@ Easy! A really lightweight and flexible URL dispatcher that just does the | ||
| Connect.createServer( | ||
| dispatch({ | ||
| '/user': { | ||
| 'GET /item': function(req, res, next){ ... }, | ||
| 'POST /item': function(req, res, next){ ... }, | ||
| } | ||
| }) | ||
| ); | ||
| ```js | ||
| Connect.createServer( | ||
| dispatch({ | ||
| '/user': { | ||
| 'GET /item': function(req, res, next){ ... }, | ||
| 'POST /item': function(req, res, next){ ... }, | ||
| } | ||
| }) | ||
| ); | ||
| ``` | ||
@@ -84,12 +112,14 @@ Just prefix the URL with the http method in uppercase followed by whitespace | ||
| dispatch({ | ||
| '/test': { | ||
| GET: function (req, res, next) { | ||
| ... | ||
| }, | ||
| POST: function (req, res, next) { | ||
| ... | ||
| } | ||
| ```js | ||
| dispatch({ | ||
| '/test': { | ||
| GET: function (req, res, next) { | ||
| ... | ||
| }, | ||
| POST: function (req, res, next) { | ||
| ... | ||
| } | ||
| }) | ||
| } | ||
| }) | ||
| ``` | ||
@@ -108,20 +138,22 @@ A couple of implementation points: | ||
| var Connect = require('connect'), | ||
| quip = require('quip'), | ||
| dispatch = require('dispatch'); | ||
| ```js | ||
| var Connect = require('connect'), | ||
| quip = require('quip'), | ||
| dispatch = require('dispatch'); | ||
| var server = Connect.createServer( | ||
| quip(), | ||
| dispatch({ | ||
| '/': function(req, res, next){ | ||
| res.text('hello world!'); | ||
| }, | ||
| '/api': function(req, res, next){ | ||
| res.json({hello: 'world'}); | ||
| } | ||
| }) | ||
| ); | ||
| var server = Connect.createServer( | ||
| quip(), | ||
| dispatch({ | ||
| '/': function(req, res, next){ | ||
| res.text('hello world!'); | ||
| }, | ||
| '/api': function(req, res, next){ | ||
| res.json({hello: 'world'}); | ||
| } | ||
| }) | ||
| ); | ||
| server.listen(8080); | ||
| server.listen(8080); | ||
| ``` | ||
| Have fun! |
@@ -261,1 +261,15 @@ var dispatch = require('../lib/dispatch'); | ||
| }; | ||
| exports['without next function - eg, vanilla http servers'] = function (test) { | ||
| var request = {url: '/test/123'}; | ||
| dispatch({ | ||
| '/test': { | ||
| '/:param': function(req, res, name){ | ||
| test.equals(req, request); | ||
| test.equals(res, 'response'); | ||
| test.equals(name, '123'); | ||
| test.done(); | ||
| } | ||
| } | ||
| })(request, 'response'); | ||
| }; |
-3
| // This file is just added for convenience so this repository can be | ||
| // directly checked out into a project's deps folder | ||
| module.exports = require('./lib/dispatch'); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
28598
82.39%7
16.67%333
4.39%1
-50%155
26.02%0
-100%1
Infinity%