本文介绍了Node.js全局需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何全局需要一个模块,以便我可以在不同的模块中使用它而不必再次需要它?
或者我每次都必须这样做吗?
有没有最佳做法?



这是我所说的一个例子。
假设我有一个像这样的 index.js

  var a = require ('a.js'),
utils = require('utils.js');

var str ='你好,这是一个测试';
str = a.change(str);

utils.return(str);

a.js

  var utils = require('utils.js'); 

exports.change = function(str){
str = str.replace('test','example');
utils.return('我把测试更改为示例!');
return str;
}

utils.js

  exports.return = function(msg){
console.log(msg);
}

你看我必须要求('utils.js')两次,但是我宁愿在index.js中要求它一次,并将它作为utils在index.js和a.js中使用。
有什么好办法可以实现吗?

解决方案

nodejs开发中的常规做法是 require()您需要的每个模块中的模块。模块本身由nodejs缓存,因此每次都返回相同的模块,并且实际上不会反复加载它。这样做而不是创建全局变量通常会使代码更加模块化和可重用,因为每个模块都唯一指定(使用 require()语句)它需要什么而不是拥有一堆模块这取决于一些预先存在的全局配置,它会产生更多的依赖关系,加载订单问题等...



可以创建一个单独的模块 require()在一堆其他模块上,然后将结果组合成一个新的主模块,如果它相关或有用,那么其他模块只需要新的主模块。 / p>

不建议使用全局变量来代替仅使用 require()语句。请记住,在nodejs编程中,大多数 require()语句只是在服务器初始化时运行,并且它们被缓存,因此没有理由避免使用<$ c的优点和模块性$ c> require()而不是全局提供。






我不会亲自推荐这是因为它模糊不清,但在您的具体示例中,您还可以将 utils 模块传递给a.js构造函数:

  var utils = require('utils.js'); 
var a = require('a.js')(utils);


how can i require a module globally so i can use it in different modules without having to require it again?or do i just have to do that everytime?is there any best practice for this?

heres an example of what i am talking about.lets say i have an index.js like this:

var a = require('a.js'),
utils = require('utils.js');

var str = 'hello this is a test';
str = a.change(str);

utils.return(str);

a.js

var utils = require('utils.js');

exports.change = function(str) {
    str = str.replace('test', 'example');
    utils.return('i changed test to example!');
    return str;
}

utils.js

exports.return = function (msg) {
    console.log(msg);
}

you see i have to require('utils.js') twice, but i would rather require it once in the index.js and have it available in the index.js and a.js as utils.is there any good way to accomplish that?

解决方案

The usual practice in nodejs development is to just require() a module in each module that you need it. The module itself is cached by nodejs so the same module is returned every time and it isn't actually loaded over and over. Doing it this way rather than creating globals generally makes code more modular and reusable since each module uniquely specifies (with require() statements) what it needs rather than having a bunch of modules that depend upon some pre-existing global configuration which creates a lot more dependencies, load order issues, etc...

It is possible to make a single module that does require() on a bunch of other modules and then combines the results into a new master module if that is relevant or useful so other modules only have to require the new master module.

It is not recommended to use globals in place of just using a require() statement. Remember that in nodejs programming, most require() statements are just run at server initialization time and they are cached so there's little reason to avoid the benefits and modularity that using require() instead of globals provides.


I wouldn't personally recommend this because it messes with modularity, but in your specific example, you could also pass the utils module to the a.js constructor:

var utils = require('utils.js');
var a = require('a.js')(utils);

这篇关于Node.js全局需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 06:13
查看更多