express里一般这样添加中间件
但我的把路由并到一起了,用了一个循环来读取
AppRoutes.forEach(route => {
app[route.method](route.path, (request: Request, response: Response, next: Function) => {
route.action(request, response)
.then(() => next)
.catch(err => next(err));
});
});
路由文件 :
import {postGetAllAction} from "./controller/PostGetAllAction";
import {postGetByIdAction} from "./controller/PostGetByIdAction";
import {postSaveAction} from "./controller/PostSaveAction";
import {getAdmin} from './controller/GetAdmin'
/**
* All application routes.
*/
export const AppRoutes = [
{
path: "/admin",
method: "get",
action: getAdmin
},
{
path: "/posts",
method: "get",
action: postGetAllAction
},
{
path: "/posts/:id",
method: "get",
action: postGetByIdAction
},
{
path: "/posts",
method: "post",
action: postSaveAction
}
];
这种情况下我怎么在循环里添加中间件?
// 多个handler 可以直接使用数组(配置中action换成数组类型),express是支持的
AppRoutes.forEach(route => {