javascript 从 node.js 的模块中调用 app.js 的函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9027866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:22:04  来源:igfitidea点击:

Call function of app.js from within module in node.js?

javascriptdesign-patternsnode.jsmodule

提问by James Simpson

Let's say I have the following app.js (obviously verysimplified):

假设我有以下 app.js(显然非常简化):

var express = require('express'),
    app = express.createServer();

// include routes
require('./lib/routes')(app);

// some random function
var foo = function() {
    return 'bar';
};

// another random function
var foo2 = function() {
    return 'bar2';
};

And then I have the routes module:

然后我有路由模块:

module.exports = function(app){
    app.get('/some/route', function(req, res){
        var fooBar = foo(),
            fooBar2 = foo2();

        res.end(fooBar + fooBar2);
    });
};

This obviously doesn't work since foo and foo2 weren't defined within the module. Is there a way to make this work, or at least a different pattern to better accomplish what this?

这显然不起作用,因为 foo 和 foo2 没有在模块中定义。有没有办法让这项工作,或者至少有不同的模式来更好地完成这项工作?

回答by Farid Nouri Neshat

Well you can just put these two functions in an object and pass them on the initialization of the routes.js .

好吧,您可以将这两个函数放在一个对象中,并将它们传递给 routes.js 的初始化。

var express = require('express'),
    app = express.createServer();

// some random function
var foo = function() {
    return 'bar';
};

// another random function
var foo2 = function() {
    return 'bar2';
};

var fns = {foo : foo, foo2: foo2}

// include routes
require('./lib/routes')(app, fns);

in routes:

在路线中:

module.exports = function(app, fns){
    app.get('/some/route', function(req, res){
        var fooBar = fns.foo(),
            fooBar2 = fns.foo2();

        res.end(fooBar + fooBar2);
    });
};

This is how would I do it. You can also include them in the app object. Beside passing them in init functions, you can also export those two functions and require them in routes.js.

这就是我要怎么做。您还可以将它们包含在 app 对象中。除了在 init 函数中传递它们之外,您还可以导出这两个函数并在 routes.js 中引入它们。

var express = require('express'),
    app = express.createServer();

// some random function
var foo = function() {
    return 'bar';
};

// another random function
var foo2 = function() {
    return 'bar2';
};

module.exports = {foo : foo, foo2: foo2}

// include routes
require('./lib/routes')(app, fns);

in routes:

在路线中:

module.exports = function(app){
    var fns = require('../app.js');
    app.get('/some/route', function(req, res){
        var fooBar = fns.foo(),
            fooBar2 = fns.foo2();

        res.end(fooBar + fooBar2);
    });
};

But I don't like the idea of it, since it makes circular dependencies. Don't have any good feelings about them.

但我不喜欢它的想法,因为它会产生循环依赖。不要对他们有任何好感。