routes
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -63,3 +63,3 @@ | ||
.replace(/([\/.])/g, '\\$1') | ||
.replace(/\*/g, '(.+)'); | ||
.replace(/\*/g, '(.*)'); | ||
return new RegExp('^' + path + '$', 'i'); | ||
@@ -66,0 +66,0 @@ }; |
{ | ||
"name": "routes", | ||
"description": "Minimalist route matching for javascript", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"homepage": "https://github.com/aaronblohowiak/routes.js", | ||
"repository": "https://github.com/aaronblohowiak/routes.js.git", | ||
"author": "Aaron Blohowiak <aaron.blohowiak@gmail.com> (http://github.com/aaronblohowiak)", | ||
"main": "index", | ||
"main": "dist/routes", | ||
"directories": { | ||
@@ -13,7 +13,11 @@ "lib": "." | ||
"scripts": { | ||
"test": "make test" | ||
"test": "make test", | ||
"prepublish": "mkdir -p dist/ && browserify --require ./index --standalone routes > dist/routes.js" | ||
}, | ||
"engines": { | ||
"node": "*" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^3.30.4" | ||
} | ||
} |
# Routes.js | ||
`routes` lets you easily dispatch based on url-style strings. It comes with a default Router function that you can use to route http requests, but it also cleanly exposes the important functionality so you could also use it to perform more generic string pattern matching. | ||
`routes` lets you easily dispatch based on url-style strings. It comes with a default `Router` function that you can use to route http requests, but it also cleanly exposes the important functionality so you could also use it to perform more generic string pattern matching. | ||
This might make it useful for things like: | ||
1. URI routing | ||
2. Cucumber-style pattern matching :) | ||
3. Routing messages by channel name from an MQ | ||
4. Dispatching hierarchical events by name | ||
* URI routing | ||
* Cucumber-style pattern matching :) | ||
* Routing messages by channel name from an MQ | ||
* Dispatching hierarchical events by name | ||
@@ -17,25 +17,32 @@ | ||
var Router = require('routes'); | ||
var router = Router(); | ||
var noop = function(){}; | ||
```js | ||
var Router = require('routes'); | ||
var router = Router(); | ||
var noop = function(){}; | ||
router.addRoute("/articles/:title?", noop); | ||
router.addRoute("/:controller/:action/:id.:format?", noop); | ||
router.addRoute("/articles/:title?", noop); | ||
router.addRoute("/:controller/:action/:id.:format?", noop); | ||
console.log(router.match("/articles")); | ||
console.log(router.match("/articles/never-gonna-let-you-down")); | ||
console.log(router.match("/posts/show/1.json")); | ||
console.log(router.match("/articles")); | ||
console.log(router.match("/articles/never-gonna-let-you-down")); | ||
console.log(router.match("/posts/show/1.json")); | ||
``` | ||
The output for `router.match("/posts/show/1.json")` would be: | ||
{ params: | ||
{ controller: 'posts', | ||
action: 'show', | ||
id: '1', | ||
format: 'json' }, | ||
splats: [], | ||
route: '/:controller/:action/:id.:format?', | ||
fn: [Function] } | ||
```js | ||
{ | ||
params: { | ||
controller: 'posts', | ||
action: 'show', | ||
id: '1', | ||
format: 'json' | ||
}, | ||
splats: [], | ||
route: '/:controller/:action/:id.:format?', | ||
fn: [Function] | ||
} | ||
``` | ||
In the example above, fn would be the function that was passed into the router. | ||
In the example above, `fn` would be the function that was passed into the router. | ||
@@ -45,8 +52,10 @@ | ||
var route = router.match("/posts/show/1.json"); | ||
route.fn.apply([req, res, route.params, route.splats]); | ||
```js | ||
var route = router.match("/posts/show/1.json"); | ||
route.fn.apply(null, [req, res, route.params, route.splats]); | ||
``` | ||
## Installation | ||
`npm install routes` | ||
npm install routes | ||
@@ -97,9 +106,9 @@ ## Path Formats | ||
`match`: takes a `String` and returns an object that contains the named `params`, `splats`, `route` (string that was matched against), and the `fn` handler you passed in with `addRoute` | ||
`match`: takes a `String` or `RegExp` and returns an object that contains the named `params`, `splats`, `route` (string that was matched against), and the `fn` handler you passed in with `addRoute` | ||
## Library API | ||
`match`: takes an array of `Routes`, and a `String`. Goes through `Routes` and returns an object for the first `Route` that matches the `String`, or 'undefined' if none is found. The result object contains `params`, `splats`, and `route`. `params` is an object containing the named matches, `splats` contains the unnamed globs ("*") and `route` contains the original string that was matched against. | ||
`match`: takes an array of `Routes`, and a `String`. It goes through `Routes` and returns an object for the first `Route` that matches the `String`, or `undefined` if none is found. The returned object contains `params`, `splats`, and `route`. `params` is an object containing the named matches, `splats` contains the unnamed globs ("*"), and `route` contains the original string that was matched against. | ||
`pathToRegExp`: takes a `path` string and an empty array, `keys`. Returns a RegExp and populates `keys` with the names of the match groups that the RegExp will match. This is largely an internal function but is provided in case someone wants to make a nifty string -> [RegExp, keys] utility. | ||
`pathToRegExp`: takes a `path` string and an empty `keys` array, returns a RegExp and populates `keys` with the names of the match groups that the RegExp will match. This is largely an internal function but is provided in case someone wants to make a nifty string -> [RegExp, keys] utility. | ||
@@ -109,5 +118,5 @@ | ||
Clone the repo, cd to it and: | ||
Clone the repo, cd to it, and: | ||
`make test` | ||
make test | ||
@@ -120,2 +129,2 @@ ## Credits | ||
This code is distributed under the MIT license, Copyright Aaron Blohowiak and TJ Holowaychuk 2011. | ||
This code is distributed under the MIT license, Copyright Aaron Blohowiak and TJ Holowaychuk 2011. |
@@ -79,2 +79,13 @@ var assert = require("assert"), | ||
{ | ||
path: "/empty/*", | ||
testMatch: { | ||
"/empty/":{ | ||
fn: noop, | ||
params: { }, | ||
splats:[""], | ||
} | ||
}, | ||
testNomatch: [ "/empty" ] | ||
}, | ||
{ | ||
path: "/whatever/*.*", | ||
@@ -81,0 +92,0 @@ testMatch: { |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
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
16390
7
471
126
0
1
3