在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:tunnckoCore/koa-better-router开源软件地址:https://github.com/tunnckoCore/koa-better-router开源编程语言:JavaScript 100.0%开源软件介绍:koa-better-router
You may also be interested in koa-rest-router. It uses this router for creating powerful, flexible and RESTful APIs for enterprise easily! Highlights
Table of Contents
(TOC generated by verb using markdown-toc) InstallInstall with npm
or install using yarn
Usage
let router = require('koa-better-router')().loadMethods()
// or
let Router = require('koa-better-router')
let router = Router() // or new Router(), no matter APIKoaBetterRouter
Params
Example let Router = require('koa-better-router')
let router = Router().loadMethods()
router.get('/', (ctx, next) => {
ctx.body = `Hello world! Prefix: ${ctx.route.prefix}`
return next()
})
// can use generator middlewares
router.get('/foobar', function * (next) {
this.body = `Foo Bar Baz! ${this.route.prefix}`
yield next
})
let api = Router({ prefix: '/api' })
// add `router`'s routes to api router
api.extend(router)
// The server
let Koa = require('koa') // Koa v2
let app = new Koa()
app.use(router.middleware())
app.use(api.middleware())
app.listen(4444, () => {
console.log('Try out /, /foobar, /api/foobar and /api')
}) .loadMethods
Example let router = require('koa-better-router')()
// all are `undefined` if you
// don't `.loadMethods` them
console.log(router.get)
console.log(router.post)
console.log(router.put)
console.log(router.del)
console.log(router.addRoute) // => function
console.log(router.middleware) // => function
console.log(router.legacyMiddleware) // => function
router.loadMethods()
console.log(router.get) // => function
console.log(router.post) // => function
console.log(router.put) // => function
console.log(router.del) // => function
console.log(router.addRoute) // => function
console.log(router.middleware) // => function
console.log(router.legacyMiddleware) // => function .createRoute
Params
Example let router = require('koa-better-router')({ prefix: '/api' })
let route = router.createRoute('GET', '/users', [
function (ctx, next) {},
function (ctx, next) {},
function (ctx, next) {},
])
console.log(route)
// => {
// prefix: '/api',
// route: '/users',
// pathname: '/users',
// path: '/api/users',
// match: matcher function against `route.path`
// method: 'GET',
// middlewares: array of middlewares for this route
// }
console.log(route.match('/foobar')) // => false
console.log(route.match('/users')) // => false
console.log(route.match('/api/users')) // => true
console.log(route.middlewares.length) // => 3 .addRoute
Params
Example let router = require('koa-better-router')()
// any number of middlewares can be given
// both modern and generator middlewares will work
router.addRoute('GET /users',
(ctx, next) => {
ctx.body = `first ${ctx.route.path};`
return next()
},
function * (next) {
this.body = `${this.body} prefix is ${this.route.prefix};`
yield next
},
(ctx, next) => {
ctx.body = `${ctx.body} and third middleware!`
return next()
}
)
// You can middlewares as array too
router.addRoute('GET', '/users/:user', [
(ctx, next) => {
ctx.body = `GET /users/${ctx.params.user}`
console.log(ctx.route)
return next()
},
function * (next) {
this.body = `${this.body}, prefix is: ${this.route.prefix}`
yield next
}
])
// can use `koa@1` and `koa@2`, both works
let Koa = require('koa')
let app = new Koa()
app.use(router.middleware())
app.listen(4290, () => {
console.log('Koa server start listening on port 4290')
}) .getRoute
Params
Example let router = require('koa-better-router')().loadMethods()
router.get('/cat/foo', function (ctx, next) {})
router.get('/baz', function (ctx, next) {})
console.log(router.getRoute('baz')) // => Route Object
console.log(router.getRoute('cat/foo')) // => Route Object
console.log(router.getRoute('/cat/foo')) // => Route Object .addRoutes
Params
Example let router = require('koa-better-router')()
// returns Route Object
let foo = router.createRoute('GET', '/foo', function (ctx, next) {
ctx.body = 'foobar'
return next()
})
console.log(foo)
let baz = router.createRoute('GET', '/baz/qux', function (ctx, next) {
ctx.body = 'baz qux'
return next()
})
console.log(baz)
// Empty array because we just
// created them, didn't include them
// as actual routes
console.log(router.routes.length) // 0
// register them as routes
router.addRoutes(foo, baz)
console.log(router.routes.length) // 2 .getRoutes
Example let router = require('koa-better-router')()
router.loadMethods()
console.log(router.routes.length) // 0
console.log(router.getRoutes().length) // 0
router.get('/foo', (ctx, next) => {})
router.get('/bar', (ctx, next) => {})
console.log(router.routes.length) // 2
console.log(router.getRoutes().length) // 2 .groupRoutes
Params
Example let router = require('./index')({ prefix: '/api/v3' })
let foo = router.createRoute('GET /foo/qux/xyz', function (ctx, next) {})
let bar = router.createRoute('GET /bar', function (ctx, next) {})
let baz = router.groupRoutes(foo, bar)
console.log(baz)
// => Route Object {
// prefix: '/api/v3',
// path: '/api/v3/foo/qux/sas/bar',
// pathname: '/foo/qux/sas/bar'
// ...
// }
// Server part
let Koa = require('koa')
let app = new Koa()
router.addRoutes(baz)
app.use(router.middleware())
app.listen(2222, () => {
console.log('Server listening on http://localhost:2222')
router.getRoutes().forEach((route) => {
console.log(`${route.method} http://localhost:2222${route.path}`)
})
}) .extend
Params
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论