expressjs之前和之后的路线方法
如何将中间件function添加到每个expressjs路由function? 大部分在数据库上都是CRUD的路由function在语句前后都有标准 – 路由function前后有没有办法。
app.route('/api/resources').all(projectsPolicy.isAllowed) .get(resources.list) .post(resources.create);
我认为这是可能的:
app.route('/api/resources').all(projectsPolicy.isAllowed) .get(before,resources.list,after) .post(before,resources.create,after);
之前和之后的function
Express支持多个callback,如
app.get('/example/b', function (req, res, next) { // do something here, like modify req or res, and then go on next(); }, function (req, res) { // get modified values here });
这也可以写成
app.route('/api/resources', projectsPolicy.isAllowed).get(...
假设中间件是isAllowed()
函数调用next()
等