标签:
koa-router
Router middleware for koa. Provides RESTful resource routing.
Router middleware for koa
npm install koa-router
Param
|
Type
|
Description
|
[opts] | Object | |
[opts.prefix] | String | prefix router paths |
var app = require(‘koa‘)(); var router = require(‘koa-router‘)(); router.get(‘/‘, function *(next) {...}); app .use(router.routes()) .use(router.allowedMethods());
router.get|put|post|patch|delete ? Router
router .get(‘/‘, function *(next) { this.body = ‘Hello World!‘; }) .post(‘/users‘, function *(next) { // ... }) .put(‘/users/:id‘, function *(next) { // ... }) .del(‘/users/:id‘, function *(next) { // ... });
router.get(‘user‘, ‘/users/:id‘, function *(next) { // ... }); router.url(‘user‘, 3); // => "/users/3"
router.get( ‘/users/:id‘, function *(next) { this.user = yield User.findOne(this.params.id); yield next; }, function *(next) { console.log(this.user); // => { id: 17, name: "Alex" } } );
var forums = new Router(); var posts = new Router(); posts.get(‘/‘, function *(next) {...}); posts.get(‘/:pid‘, function *(next) {...}); forums.use(‘/forums/:fid/posts‘, posts.routes(), posts.allowedMethods()); // responds to "/forums/123/posts" and "/forums/123/posts/123" app.use(forums.routes());
var router = new Router({ prefix: ‘/users‘ }); router.get(‘/‘, ...); // responds to "/users" router.get(‘/:id‘, ...); // responds to "/users/:id"
router.get(‘/:category/:title‘, function *(next) { console.log(this.params); // => { category: ‘programming‘, title: ‘how-to-node‘ } });
Param
|
Type
|
Description
|
path | String | |
[middleware] | function | route middleware(s) |
callback | function | route callback |
router.use([path], middleware, [...]) ? Router
Param
|
Type
|
[path] | String |
middleware | function |
[...] | function |
router.use(session(), authorize()); // use middleware only with given path router.use(‘/users‘, userAuth()); app.use(router.routes());
router.prefix(prefix) ? Router
Param
|
Type
|
prefix | String |
router.prefix(‘/things/:thing_id‘)
router.allowedMethods([options]) ? function
Param
|
Type
|
Description
|
[options] | Object | |
[options.throw] | Boolean | throw error instead of setting status and header |
[options.notImplemented] | Function | throw the returned value in place of the default NotImplemented error |
[options.methodNotAllowed] | Function | throw the returned value in place of the default MethodNotAllowed error |
var app = koa(); var router = router(); app.use(router.routes()); app.use(router.allowedMethods());
var app = koa(); var router = router(); var Boom = require(‘boom‘); app.use(router.routes()); app.use(router.allowedMethods({ throw: true, notImplemented: () => new Boom.notImplemented(), methodNotAllowed: () => new Boom.methodNotAllowed() }));
router.redirect(source, destination, code) ? Router
router.redirect(‘/login‘, ‘sign-in‘);
router.all(‘/login‘, function *() { this.redirect(‘/sign-in‘); this.status = 301; });
Param
|
Type
|
Description
|
source | String | URL or route name. |
destination | String | URL or route name. |
code | Number | HTTP status code (default: 301). |
router.route(name) ? Layer | false
Param
|
Type
|
name | String |
router.url(name, params) ? String | Error
KoaHub平台基于Node.js开发的Koa router路由插件代码信息详情
标签:
原文地址:http://www.cnblogs.com/jycxqe/p/5974232.html