Skip to content

Commit 9d829da

Browse files
committed
1 parent dbaa56e commit 9d829da

File tree

5 files changed

+73
-14
lines changed

5 files changed

+73
-14
lines changed

README.md

+45
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,51 @@ router.route('/pet/:id')
382382
server.listen(8080)
383383
```
384384

385+
## Migrating to 2.x from 1.x
386+
387+
The main change is the update to `[email protected]`, which has a few breaking changes:
388+
389+
#### No longer a direct conversion to a RegExp with sugar on top.
390+
391+
It's a path matcher with named and unnamed matching groups. It's unlikely you previously abused this feature,
392+
it's rare and you could always use a RegExp instead. An example of this would be:
393+
394+
```javascript
395+
// Used to work
396+
router.get('/\\d+')
397+
398+
// Now requires matching group
399+
router.get('/(\\d+)')
400+
```
401+
402+
#### All matching RegExp special characters can be used in a matching group.
403+
404+
Other RegExp features are not supported - no nested matching groups, non-capturing groups or look aheads
405+
There is really only one common change is needing replacing any routes with `*` to `(.*)`. Some examples:
406+
407+
- `/:user(*)` becomes `/:user(.*)`
408+
- `/:user/*` becomes `/:user/(.*)`
409+
- `/foo/*/bar` becomes `/foo/(.*)/bar`
410+
411+
#### Parameters have suffixes that augment meaning - `*`, `+` and `?`. E.g. `/:user*`
412+
413+
Needs more info.
414+
415+
#### Named params with regex no longer define positionally.
416+
417+
One other small change (hopefully low impact), is that named parameters with regular expressions no longer result in positional
418+
values in the `params` object. An example is:
419+
420+
```javascript
421+
router.get('/:foo(.*)')
422+
423+
// old GET /bar
424+
console.log(req.params) // {0: 'bar', 'foo': 'bar'}
425+
426+
// new GET /bar
427+
console.log(req.params) // {'foo': 'bar'}
428+
```
429+
385430
## License
386431

387432
[MIT](LICENSE)

lib/layer.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,28 @@ var hasOwnProperty = Object.prototype.hasOwnProperty
2828

2929
module.exports = Layer
3030

31-
function Layer(path, options, fn) {
31+
function Layer(p, options, fn) {
3232
if (!(this instanceof Layer)) {
33-
return new Layer(path, options, fn)
33+
return new Layer(p, options, fn)
3434
}
3535

3636
debug('new %o', path)
3737
var opts = options || {}
3838

39+
// If not in strict allow both with or without trailing slash
40+
var path = p
41+
if (!opts.strict) {
42+
if (!Array.isArray(path) && path !== '/' && path[path.length - 1] === '/') {
43+
path = path.substr(0, path.length - 1)
44+
} else {
45+
for (var i = 0; i < path.length; i++) {
46+
if (path[i] !== '/' && path[i][path[i].length - 1] === '/') {
47+
path[i] = path[i].substr(0, path[i].length - 1)
48+
}
49+
}
50+
}
51+
}
52+
3953
this.handle = fn
4054
this.name = fn.name || '<anonymous>'
4155
this.params = undefined

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"debug": "3.1.0",
1414
"methods": "~1.1.2",
1515
"parseurl": "~1.3.2",
16-
"path-to-regexp": "0.1.7",
16+
"path-to-regexp": "^2.4.0",
1717
"setprototypeof": "1.1.0",
1818
"utils-merge": "1.0.1"
1919
},

test/req.params.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe('req.params', function () {
137137
})
138138
})
139139

140-
router.get('/*', hitParams(1))
140+
router.get('/(.*)', hitParams(1))
141141

142142
request(server)
143143
.get('/buzz')
@@ -156,7 +156,7 @@ describe('req.params', function () {
156156
})
157157
})
158158

159-
router.get('/*', hitParams(1))
159+
router.get('/(.*)', hitParams(1))
160160

161161
request(server)
162162
.get('/bar')

test/route.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ describe('Router', function () {
716716

717717
it('should capture everything with pre- and post-fixes', function (done) {
718718
var router = new Router()
719-
var route = router.route('/foo/*/bar')
719+
var route = router.route('/foo/(.*)/bar')
720720
var server = createServer(router)
721721

722722
route.all(sendParams)
@@ -728,7 +728,7 @@ describe('Router', function () {
728728

729729
it('should capture greedly', function (done) {
730730
var router = new Router()
731-
var route = router.route('/foo/*/bar')
731+
var route = router.route('/foo/(.*)/bar')
732732
var server = createServer(router)
733733

734734
route.all(sendParams)
@@ -740,7 +740,7 @@ describe('Router', function () {
740740

741741
it('should be an optional capture', function (done) {
742742
var router = new Router()
743-
var route = router.route('/foo*')
743+
var route = router.route('/foo(.*)')
744744
var server = createServer(router)
745745

746746
route.all(sendParams)
@@ -753,7 +753,7 @@ describe('Router', function () {
753753
it('should require preceeding /', function (done) {
754754
var cb = after(2, done)
755755
var router = new Router()
756-
var route = router.route('/foo/*')
756+
var route = router.route('/foo/(.*)')
757757
var server = createServer(router)
758758

759759
route.all(sendParams)
@@ -770,23 +770,23 @@ describe('Router', function () {
770770
it('should work in a named parameter', function (done) {
771771
var cb = after(2, done)
772772
var router = new Router()
773-
var route = router.route('/:foo(*)')
773+
var route = router.route('/:foo(.*)')
774774
var server = createServer(router)
775775

776776
route.all(sendParams)
777777

778778
request(server)
779779
.get('/bar')
780-
.expect(200, {'0': 'bar', 'foo': 'bar'}, cb)
780+
.expect(200, {'foo': 'bar'}, cb)
781781

782782
request(server)
783783
.get('/fizz/buzz')
784-
.expect(200, {'0': 'fizz/buzz', 'foo': 'fizz/buzz'}, cb)
784+
.expect(200, {'foo': 'fizz/buzz'}, cb)
785785
})
786786

787787
it('should work before a named parameter', function (done) {
788788
var router = new Router()
789-
var route = router.route('/*/user/:id')
789+
var route = router.route('/(.*)/user/:id')
790790
var server = createServer(router)
791791

792792
route.all(sendParams)
@@ -799,7 +799,7 @@ describe('Router', function () {
799799
it('should work within arrays', function (done) {
800800
var cb = after(3, done)
801801
var router = new Router()
802-
var route = router.route(['/user/:id', '/foo/*', '/:action'])
802+
var route = router.route(['/user/:id', '/foo/(.*)', '/:action'])
803803
var server = createServer(router)
804804

805805
route.all(sendParams)

0 commit comments

Comments
 (0)