Skip to content

Commit 3ca5aa6

Browse files
authored
Fix: Ensure .use() middleware works when path or prefix contains parameters (#203)
* fix: parameter in path of .use * fix: parameter in prefix
1 parent bc67eef commit 3ca5aa6

File tree

4 files changed

+42
-38
lines changed

4 files changed

+42
-38
lines changed

lib/layer.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module.exports = class Layer {
4444

4545
if (this.opts.pathAsRegExp === true) {
4646
this.regexp = new RegExp(path);
47-
} else if (this.path) {
47+
} else {
4848
if ('strict' in this.opts) {
4949
// path-to-regexp renamed strict to trailing in v8.1.0
5050
this.opts.trailing = this.opts.strict !== true;
@@ -224,18 +224,16 @@ module.exports = class Layer {
224224
* @private
225225
*/
226226
setPrefix(prefix) {
227-
if (this.path) {
228-
this.path =
229-
this.path !== '/' || this.opts.strict === true
230-
? `${prefix}${this.path}`
231-
: prefix;
232-
if (this.opts.pathAsRegExp === true || prefix instanceof RegExp) {
233-
this.regexp = new RegExp(this.path);
234-
} else if (this.path) {
235-
const { regexp, keys } = pathToRegexp(this.path, this.opts);
236-
this.regexp = regexp;
237-
this.paramNames = keys;
238-
}
227+
this.path =
228+
this.path !== '/' || this.opts.strict === true
229+
? `${prefix}${this.path}`
230+
: prefix;
231+
if (this.opts.pathAsRegExp === true || prefix instanceof RegExp) {
232+
this.regexp = new RegExp(this.path);
233+
} else if (this.path) {
234+
const { regexp, keys } = pathToRegexp(this.path, this.opts);
235+
this.regexp = regexp;
236+
this.paramNames = keys;
239237
}
240238

241239
return this;

lib/router.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ class Router {
168168
const routerPrefixHasParam = Boolean(
169169
router.opts.prefix && keys.length > 0
170170
);
171-
router.register(path || '([^/]*)', [], m, {
171+
router.register(path || '', [], m, {
172172
end: false,
173173
ignoreCaptures: !hasPath && !routerPrefixHasParam,
174-
pathAsRegExp: true
174+
pathAsRegExp: hasPath ? path instanceof RegExp : false
175175
});
176176
}
177177
}

test/lib/layer.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -291,34 +291,16 @@ describe('Layer', () => {
291291
});
292292
assert.strictEqual(url, '/programming/how%20to%20node%20%26%20js%2Fts');
293293
});
294-
295-
it('setPrefix method checks Layer for path', () => {
296-
const route = new Layer('/category', ['get'], [() => {}], {
297-
name: 'books'
298-
});
299-
route.path = '/hunter2';
300-
const prefix = route.setPrefix('TEST');
301-
assert.strictEqual(prefix.path, 'TEST/hunter2');
302-
});
303294
});
304295

305296
describe('Layer#prefix', () => {
306-
it('setPrefix method passes check Layer for path', () => {
307-
const route = new Layer('/category', ['get'], [() => {}], {
297+
it('setPrefix method checks Layer for path', () => {
298+
const route = new Layer('/category', ['get'], [() => { }], {
308299
name: 'books'
309300
});
310301
route.path = '/hunter2';
311302
const prefix = route.setPrefix('/TEST');
312303
assert.strictEqual(prefix.path, '/TEST/hunter2');
313304
});
314-
315-
it('setPrefix method fails check Layer for path', () => {
316-
const route = new Layer(false, ['get'], [() => {}], {
317-
name: 'books'
318-
});
319-
route.path = false;
320-
const prefix = route.setPrefix('/TEST');
321-
assert.strictEqual(prefix.path, false);
322-
});
323305
});
324306
});

test/lib/router.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,27 @@ describe('Router#use()', () => {
10481048
assert.strictEqual(res.body.foobar, 'foobar');
10491049
});
10501050

1051+
it('uses router middleware at given path with parameters - koajs/router#gh-202', async () => {
1052+
const app = new Koa();
1053+
const router = new Router();
1054+
router.use('/:foo/:bar', (ctx, next) => {
1055+
ctx.foo = ctx.params.foo;
1056+
ctx.bar = ctx.params.bar;
1057+
return next();
1058+
});
1059+
router.get('/:foo/:bar', (ctx) => {
1060+
ctx.body = {
1061+
foobar: ctx.foo + ctx.bar
1062+
};
1063+
});
1064+
app.use(router.routes());
1065+
const res = await request(http.createServer(app.callback()))
1066+
.get('/qux/baz')
1067+
.expect(200);
1068+
1069+
assert.strictEqual(res.body.foobar, 'quxbaz');
1070+
});
1071+
10511072
it('runs router middleware before subrouter middleware', async () => {
10521073
const app = new Koa();
10531074
const router = new Router();
@@ -1911,17 +1932,20 @@ describe('Router#prefix', () => {
19111932
assert.strictEqual('params' in ctx, true);
19121933
assert.strictEqual(typeof ctx.params, 'object');
19131934
assert.strictEqual(ctx.params.category, 'cats');
1935+
ctx.category = ctx.params.category;
19141936
return next();
19151937
})
19161938
.get('/suffixHere', (ctx) => {
19171939
assert.strictEqual('params' in ctx, true);
19181940
assert.strictEqual(typeof ctx.params, 'object');
19191941
assert.strictEqual(ctx.params.category, 'cats');
1920-
ctx.status = 204;
1942+
ctx.status = 200;
1943+
ctx.body = ctx.category;
19211944
});
1922-
await request(http.createServer(app.callback()))
1945+
const res = await request(http.createServer(app.callback()))
19231946
.get('/cats/suffixHere')
1924-
.expect(204);
1947+
.expect(200);
1948+
assert.strictEqual(res.text, 'cats');
19251949
});
19261950

19271951
it('populates ctx.params correctly for more complex router prefix (including use)', async () => {

0 commit comments

Comments
 (0)