express typescript这情况中怎么加中间件?

下面是我之前的写法:要想加中间件很方便,
userValidator.login,userCtrl.login,auth都是我自己加的中间件

//用户登陆
router.post("/user/login",userValidator.login,userCtrl.login)
//用户注册
router.post("/users",userValidator.register,userCtrl.register)
//获取当前登陆用户
router.get("/user",auth,userCtrl.getCurrentUser)

下面是我新找的一个项目:路由是循环出来的:

 // register all application routes
    AppRoutes.forEach(route => {
        app[route.method](route.path, (request: Request, response: Response, next: Function) => {
            route.action(request, response)
                .then(() => next)
                .catch(err => next(err));
        });
    });

路由文件:

export const AppRoutes = [
    {
        path: "/posts",
        method: "get",
        action: postGetAllAction
    },
    {
        path: "/posts/:id",
        method: "get",
        action: postGetByIdAction
    },
    {
        path: "/posts",
        method: "post",
        action: postSaveAction
    }
];

这我想再给中间加中间件要怎么弄呀???

阅读 664
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进