|
|
@ -32,9 +32,9 @@ class Router { |
|
|
|
* 注册GET路由,支持中间件链 |
|
|
|
* @param {string} path - 路由路径 |
|
|
|
* @param {Function} handler - 中间件和处理函数 |
|
|
|
* @param {...Object} others - 其他参数(可选) |
|
|
|
* @param {Object} others - 其他参数(可选) |
|
|
|
*/ |
|
|
|
get(path, handler, ...others) { |
|
|
|
get(path, handler, others) { |
|
|
|
this._registerRoute("get", path, handler, others) |
|
|
|
} |
|
|
|
|
|
|
@ -42,23 +42,23 @@ class Router { |
|
|
|
* 注册POST路由,支持中间件链 |
|
|
|
* @param {string} path - 路由路径 |
|
|
|
* @param {Function} handler - 中间件和处理函数 |
|
|
|
* @param {...Object} others - 其他参数(可选) |
|
|
|
* @param {Object} others - 其他参数(可选) |
|
|
|
*/ |
|
|
|
post(path, handler, ...others) { |
|
|
|
post(path, handler, others) { |
|
|
|
this._registerRoute("post", path, handler, others) |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 注册PUT路由,支持中间件链 |
|
|
|
*/ |
|
|
|
put(path, handler, ...others) { |
|
|
|
put(path, handler, others) { |
|
|
|
this._registerRoute("put", path, handler, others) |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 注册DELETE路由,支持中间件链 |
|
|
|
*/ |
|
|
|
delete(path, handler, ...others) { |
|
|
|
delete(path, handler, others) { |
|
|
|
this._registerRoute("delete", path, handler, others) |
|
|
|
} |
|
|
|
|
|
|
@ -89,6 +89,7 @@ class Router { |
|
|
|
// 组合全局中间件、路由专属中间件和 handler
|
|
|
|
const middlewares = [...this.middlewares]; |
|
|
|
if (route) { |
|
|
|
// 如果匹配到路由,添加路由专属中间件和处理函数
|
|
|
|
ctx.params = route.params; |
|
|
|
|
|
|
|
let isAuth = this.options.auth; |
|
|
@ -98,13 +99,13 @@ class Router { |
|
|
|
|
|
|
|
middlewares.push(RouteAuth({ auth: isAuth })); |
|
|
|
middlewares.push(route.handler) |
|
|
|
// 用 koa-compose 组合
|
|
|
|
const composed = compose(middlewares); |
|
|
|
await composed(ctx, next); |
|
|
|
} else { |
|
|
|
middlewares.push(RouteAuth({ auth: this.options.auth })); |
|
|
|
// 如果没有匹配到路由,直接调用 next
|
|
|
|
await next(); |
|
|
|
} |
|
|
|
|
|
|
|
// 用 koa-compose 组合
|
|
|
|
const composed = compose(middlewares); |
|
|
|
await composed(ctx, next); |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|