来自node.js内部模块的app.js调用函数? [英] Call function of app.js from within module in node.js?

查看:118
本文介绍了来自node.js内部模块的app.js调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

//一些随机函数
var foo = function(){
return'bar';
};

//另一个随机函数
var foo2 = function(){
return'bar2';
};

然后我有路由模块:

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

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

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

解决方案

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

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

//一些随机函数
var foo = function(){
return'bar';
};

//另一个随机函数
var foo2 = function(){
return'bar2';
};

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

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

路线:

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

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

这是怎么做到的。您也可以将它们包含在应用对象中。除了将它们传递给init函数之外,还可以导出这两个函数,并在routes.js中要求它们。

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

//一些随机函数
var foo = function(){
return'bar';
};

//另一个随机函数
var foo2 = function(){
return'bar2';
};

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

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

路线:

  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);
});
};

但是我不喜欢它的想法,因为它产生循环依赖。对他们没有什么好感。


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

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?

解决方案

Well you can just put these two functions in an object and pass them on the initialization of the 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.

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.

这篇关于来自node.js内部模块的app.js调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆