Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用';这';中间件中的上下文_Node.js_Express_Connect_Middleware - Fatal编程技术网

Node.js 如何使用';这';中间件中的上下文

Node.js 如何使用';这';中间件中的上下文,node.js,express,connect,middleware,Node.js,Express,Connect,Middleware,出于我的目的,我编写了自己的中间件作为模块,如下所示: -- myMiddleware.js module.exports = { fn1: function (req, res, next) { console.log('fn1'); next(); }, fn2: function (req, res, next) { console.log('fn2'); this.fn1(req, res, func

出于我的目的,我编写了自己的中间件作为模块,如下所示:

-- myMiddleware.js

module.exports = {
    fn1: function (req, res, next) {
       console.log('fn1');
       next();
    },

    fn2: function (req, res, next) {
        console.log('fn2');
        this.fn1(req, res, function () {
             next();
        });
    }
};
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));

app.use(require('./myMiddleware').fn2);
在我的sserver.js中,我使用该中间件如下:

-- myMiddleware.js

module.exports = {
    fn1: function (req, res, next) {
       console.log('fn1');
       next();
    },

    fn2: function (req, res, next) {
        console.log('fn2');
        this.fn1(req, res, function () {
             next();
        });
    }
};
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));

app.use(require('./myMiddleware').fn2);

不幸的是,这不起作用,因为fn2中的上下文不是myMiddleware.js对象。如何使用正确的this?

如果不能,则“this”属性将始终是香草服务器(http.Server)实例或兼容的处理程序

您可以这样做:

var middlewares = {};

module.exports = middlewares;

middlewares.fn1 = function(req, res, next){ ... };

middlewares.fn2 = function(req, res, next){ 
    middlewares.fn1(req, res, next); 
};

这不是因为中间商。
当函数在javascript中作为参数传递时,总是会丢失上下文。Express将中间件作为参数传递。这就是为什么

有几种技术可以绑定上下文,您可以使用apply、call、closure或更简单的new。 这是一个使用闭包的示例:
[edit]修复示例中的打字错误,并添加功能代码示例

middle.js

module.exports = {
    fn1: function(req, res, next) {
       console.log('fn1');
       next();
    },

    fn2: function(req, res, next) {
        var self = this;
        return function (req, res, next) {
            console.log('fn2');
            self.fn1(req, res, function () {
                next();
            });
        }
     }
};
js(注意fn2())


通过谷歌搜索“javascritp函数绑定上下文”,您可以找到好文章,更好地理解这种行为

为什么我不能只使用var middleware=module.exports={}?你可以!,您还可以跳过Middleware引用并使用exports.fn1=。。。;exports.fn2=函数(…){exports.fn1();};这取决于你感觉如何更舒适。这是这个问题的正确答案。OP接受的答案无法解释