Add koa-router's Router to koa's Context (#27472)

This commit is contained in:
Romain Faust
2018-07-27 17:54:15 +02:00
committed by Andy
parent d997ab0681
commit 11f441840c
2 changed files with 26 additions and 15 deletions

View File

@@ -3,6 +3,7 @@
// Definitions by: Jerry Chin <https://github.com/hellopao>
// Pavel Ivanov <https://github.com/schfkt>
// JounQin <https://github.com/JounQin>
// Romain Faust <https://github.com/romain-faust>
// Definitions: https://github.com/hellopao/DefinitelyTyped
// TypeScript Version: 2.3
@@ -51,6 +52,10 @@ declare namespace Router {
* url params
*/
params: any;
/**
* the router instance
*/
router: Router;
}
export interface IMiddleware {

View File

@@ -8,21 +8,27 @@ const router = new Router({
});
router
.param('id', function(id, ctx, next) {
next();
})
.get('/', function (ctx, next) {
ctx.body = 'Hello World!';
})
.post('/users', function (ctx, next) {
// ...
})
.put('/users/:id', function (ctx, next) {
ctx.body = ctx.params.id;
})
.del('/users/:id', function () {
// ...
});
.param('id', function (id, ctx, next) {
next();
})
.get('/', function (ctx, next) {
ctx.body = 'Hello World!';
})
.get('/users/:id', function (ctx, next) {
ctx.body = ctx.router.url('user-accounts', { id: ctx.params.id })
})
.get('user-accounts', '/users/:id/accounts', function (ctx, next) {
// ...
})
.post('/users', function (ctx, next) {
// ...
})
.put('/users/:id', function (ctx, next) {
ctx.body = ctx.params.id;
})
.del('/users/:id', function () {
// ...
});
router.get('user', '/users/:id', function (ctx) {
ctx.body = "sdsd";