Skip to content

Commit

Permalink
Merge 10ca7fe into 27b75fc
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleytodd committed Jun 19, 2018
2 parents 27b75fc + 10ca7fe commit 002b9fd
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 14 deletions.
45 changes: 45 additions & 0 deletions README.md
Expand Up @@ -332,6 +332,51 @@ curl http://127.0.0.1:8080/such_path
> such_path
```

## Migrating to 2.x from 1.x

The main change is the update to `path-to-regexp@2.0.0`, which has a few breaking changes:

#### No longer a direct conversion to a RegExp with sugar on top.

It's a path matcher with named and unnamed matching groups. It's unlikely you previously abused this feature,
it's rare and you could always use a RegExp instead. An example of this would be:

```javascript
// Used to work
router.get('/\\d+')

// Now requires matching group
router.get('/(\\d+)')
```

#### All matching RegExp special characters can be used in a matching group.

Other RegExp features are not supported - no nested matching groups, non-capturing groups or look aheads
There is really only one common change is needing replacing any routes with `*` to `(.*)`. Some examples:

- `/:user(*)` becomes `/:user(.*)`
- `/:user/*` becomes `/:user/(.*)`
- `/foo/*/bar` becomes `/foo/(.*)/bar`

#### Parameters have suffixes that augment meaning - `*`, `+` and `?`. E.g. `/:user*`

Needs more info.

#### Named params with regex no longer define positionally.

One other small change (hopefully low impact), is that named parameters with regular expressions no longer result in positional
values in the `params` object. An example is:

```javascript
router.get('/:foo(.*)')

// old GET /bar
console.log(req.params) // {0: 'bar', 'foo': 'bar'}

// new GET /bar
console.log(req.params) // {'foo': 'bar'}
```

## License

[MIT](LICENSE)
Expand Down
18 changes: 16 additions & 2 deletions lib/layer.js
Expand Up @@ -28,14 +28,28 @@ var hasOwnProperty = Object.prototype.hasOwnProperty

module.exports = Layer

function Layer(path, options, fn) {
function Layer(p, options, fn) {
if (!(this instanceof Layer)) {
return new Layer(path, options, fn)
return new Layer(p, options, fn)
}

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

// If not in strict allow both with or without trailing slash
var path = p
if (!opts.strict) {
if (!Array.isArray(path) && path !== '/' && path[path.length - 1] === '/') {
path = path.substr(0, path.length - 1)
} else {
for (var i = 0; i < path.length; i++) {
if (path[i] !== '/' && path[i][path[i].length - 1] === '/') {
path[i] = path[i].substr(0, path[i].length - 1)
}
}
}
}

this.handle = fn
this.name = fn.name || '<anonymous>'
this.params = undefined
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -13,7 +13,7 @@
"debug": "2.6.9",
"methods": "~1.1.2",
"parseurl": "~1.3.2",
"path-to-regexp": "0.1.7",
"path-to-regexp": "2.1.0",
"setprototypeof": "1.1.0",
"utils-merge": "1.0.1"
},
Expand Down
4 changes: 2 additions & 2 deletions test/req.params.js
Expand Up @@ -137,7 +137,7 @@ describe('req.params', function () {
})
})

router.get('/*', hitParams(1))
router.get('/(.*)', hitParams(1))

request(server)
.get('/buzz')
Expand All @@ -156,7 +156,7 @@ describe('req.params', function () {
})
})

router.get('/*', hitParams(1))
router.get('/(.*)', hitParams(1))

request(server)
.get('/bar')
Expand Down
18 changes: 9 additions & 9 deletions test/route.js
Expand Up @@ -579,7 +579,7 @@ describe('Router', function () {

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

route.all(sendParams)
Expand All @@ -591,7 +591,7 @@ describe('Router', function () {

it('should capture greedly', function (done) {
var router = new Router()
var route = router.route('/foo/*/bar')
var route = router.route('/foo/(.*)/bar')
var server = createServer(router)

route.all(sendParams)
Expand All @@ -603,7 +603,7 @@ describe('Router', function () {

it('should be an optional capture', function (done) {
var router = new Router()
var route = router.route('/foo*')
var route = router.route('/foo(.*)')
var server = createServer(router)

route.all(sendParams)
Expand All @@ -616,7 +616,7 @@ describe('Router', function () {
it('should require preceeding /', function (done) {
var cb = after(2, done)
var router = new Router()
var route = router.route('/foo/*')
var route = router.route('/foo/(.*)')
var server = createServer(router)

route.all(sendParams)
Expand All @@ -633,23 +633,23 @@ describe('Router', function () {
it('should work in a named parameter', function (done) {
var cb = after(2, done)
var router = new Router()
var route = router.route('/:foo(*)')
var route = router.route('/:foo(.*)')
var server = createServer(router)

route.all(sendParams)

request(server)
.get('/bar')
.expect(200, {'0': 'bar', 'foo': 'bar'}, cb)
.expect(200, {'foo': 'bar'}, cb)

request(server)
.get('/fizz/buzz')
.expect(200, {'0': 'fizz/buzz', 'foo': 'fizz/buzz'}, cb)
.expect(200, {'foo': 'fizz/buzz'}, cb)
})

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

route.all(sendParams)
Expand All @@ -662,7 +662,7 @@ describe('Router', function () {
it('should work within arrays', function (done) {
var cb = after(3, done)
var router = new Router()
var route = router.route(['/user/:id', '/foo/*', '/:action'])
var route = router.route(['/user/:id', '/foo/(.*)', '/:action'])
var server = createServer(router)

route.all(sendParams)
Expand Down

0 comments on commit 002b9fd

Please sign in to comment.