find-my-way
Advanced tools
+2
-2
@@ -198,3 +198,3 @@ 'use strict' | ||
| while (i < pathLen && path[i] !== '/') i++ | ||
| params[pindex++] = path.slice(0, i) | ||
| params[pindex++] = decodeURIComponent(path.slice(0, i)) | ||
| path = path.slice(i) | ||
@@ -207,3 +207,3 @@ continue | ||
| if (node) { | ||
| params[pindex] = path | ||
| params[pindex] = decodeURIComponent(path) | ||
| currentNode = node | ||
@@ -210,0 +210,0 @@ path = '' |
+1
-1
| { | ||
| "name": "find-my-way", | ||
| "version": "0.2.0", | ||
| "version": "0.2.1", | ||
| "description": "Crazy fast http radix based router", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+11
-4
@@ -51,3 +51,3 @@ # find-my-way | ||
| #### on(method, path, handler, [store]) | ||
| Register a new route, `store` is an object that you can access later inside the handler function. | ||
| Register a new route, `store` is an object that you can access later inside the handler function. | ||
| ```js | ||
@@ -60,6 +60,13 @@ router.on('GET', '/', (req, res, params) => { | ||
| router.on('GET', '/store', (req, res, params, store) => { | ||
| // the store can be updated | ||
| assert.equal(store, { hello: 'world' }) | ||
| }, { hello: 'world' }) | ||
| ``` | ||
| If you want to register a **parametric** path, just use the *colon* before the parameter name, if you need a **wildcard** use the *star*. | ||
| ```js | ||
| // parametric | ||
| router.on('GET', '/example/:name', () => {})) | ||
| // wildcard | ||
| router.on('GET', '/other-example/*', () => {})) | ||
| ``` | ||
| <a name="lookup"></a> | ||
@@ -69,3 +76,3 @@ #### lookup(request, response) | ||
| If a route is found it will automatically called the handler, otherwise the default route will be called. | ||
| The url is sanitized internally. | ||
| The url is sanitized internally, all the parameters and wildcards are decoded automatically. | ||
| ```js | ||
@@ -78,3 +85,3 @@ router.lookup(req, res) | ||
| Return (if present) the route registered in *method:path*. | ||
| The path must be sanitized. | ||
| The path must be sanitized, all the parameters and wildcards are decoded automatically. | ||
| ```js | ||
@@ -81,0 +88,0 @@ router.find('GET', '/example') |
+20
-0
@@ -227,1 +227,21 @@ 'use strict' | ||
| }) | ||
| test('should decode the uri - parametric', t => { | ||
| t.plan(1) | ||
| const findMyWay = FindMyWay() | ||
| const fn = () => {} | ||
| findMyWay.on('GET', '/test/:id', fn) | ||
| t.deepEqual(findMyWay.find('GET', '/test/he%2Fllo'), { handler: fn, params: { id: 'he/llo' }, store: null }) | ||
| }) | ||
| test('should decode the uri - wildcard', t => { | ||
| t.plan(1) | ||
| const findMyWay = FindMyWay() | ||
| const fn = () => {} | ||
| findMyWay.on('GET', '/test/*', fn) | ||
| t.deepEqual(findMyWay.find('GET', '/test/he%2Fllo'), { handler: fn, params: { '*': 'he/llo' }, store: null }) | ||
| }) |
+19
-0
@@ -31,1 +31,20 @@ 'use strict' | ||
| }) | ||
| test('update the store', t => { | ||
| t.plan(2) | ||
| const findMyWay = FindMyWay() | ||
| var bool = false | ||
| findMyWay.on('GET', '/test', (req, res, params, store) => { | ||
| if (!bool) { | ||
| t.is(store.hello, 'world') | ||
| store.hello = 'hello' | ||
| bool = true | ||
| findMyWay.lookup({ method: 'GET', url: '/test' }, null) | ||
| } else { | ||
| t.is(store.hello, 'hello') | ||
| } | ||
| }, { hello: 'world' }) | ||
| findMyWay.lookup({ method: 'GET', url: '/test' }, null) | ||
| }) |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
26845
5.73%686
4.57%102
7.37%