在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:koajs/trie-router开源软件地址:https://github.com/koajs/trie-router开源编程语言:JavaScript 100.0%开源软件介绍:Koa Trie RouterAboutTrie routing for Koa based on routington. Routes are orthogonal and strict, so the order of definition doesn't matter. See routington for more details. Versions
Features
NotesThe router handles Usageconst Koa = require('koa')
const Router = require('koa-trie-router')
let app = new Koa()
let router = new Router()
router
.use(function(ctx, next) {
console.log('* requests')
return next()
})
.get(function(ctx, next) {
console.log('GET requests')
return next()
})
.put('/foo', function (ctx) {
ctx.body = 'PUT /foo requests'
})
.post('/bar', function (ctx) {
ctx.body = 'POST /bar requests'
})
app.use(router.middleware())
app.listen(3000) APIrouter.use(middleware...)Handles all requests router.use(function(ctx) {
ctx.body = 'test' // All requests
}) router[method](middleware...)Handles requests only by one HTTP method router.get(function(ctx) {
ctx.body = 'GET' // GET requests
}) router[method](paths, middleware...)Handles requests only by one HTTP method and one route Where
Signature router
.get('/one', middleware)
.post(['/two','/three'], middleware)
.put(['/four'], [middleware, middleware])
.del('/five', middleware, middleware, middleware) router.middleware()Like Express, all routes belong to a single middleware. You can use const Koa = require('koa')
const mount = require('koa-mount')
const Router = require('koa-trie-router')
let app = new Koa()
let router1 = new Router()
let router2 = new Router()
router1.get('/foo', middleware)
router2.get('/bar', middleware)
app.use(mount('/foo', router1.middleware()))
app.use(mount('/bar', router2.middleware())) router.isImplementedMethod(method)Checks if the server implements a particular method and returns app.use(function(ctx, next) {
if (!router.isImplementedMethod(ctx.method)) {
ctx.status = 501
return
}
return next()
}) ctx.request.params
router.get('/user/:name', async function (ctx, next) {
let name = ctx.request.params.name // or ctx.params.name
let user = await User.get(name)
return next()
}) Error handlingThe middleware throws an error with Path DefinitionsFor path definitions, see routington. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论