小编Ele*_*to0的帖子

将参数从中间件传递到表达路由器

这个问题很长一段时间困扰着我,我从来没有找到一个能够清楚地向我解释的答案.

我正在使用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)

javascript node.js express

2
推荐指数
1
解决办法
1621
查看次数

标签 统计

express ×1

javascript ×1

node.js ×1