这个问题很长一段时间困扰着我,我从来没有找到一个能够清楚地向我解释的答案.
我正在使用Express 4.0,我已经制作了这个片段,向您展示我所做的解决方案,以获得我所需要的结果.
我想将参数req.method和一个路由数组controllers.routes直接传递到快速路由器,以定义方法并过滤中间件中的路由.
var controller = require( './controllers' );
module.exports = function ( app ){
app.router.use(function ( req, res, next ){
// function with the args before router get called
anotherFn( app, req.method, controller );
// Call the express router after called function with args
next( 'route' );
});
};
function anotherFn( app, method, controller ){
// Express router called with args here...
app.router[ method ]( controllers.routes,
function ( req, res, next ){
try{
controller[ …Run Code Online (Sandbox Code Playgroud)