简体   繁体   English

多个调用方向的模块的节点模块

[英]node module of modules multiple calling directions

Ok, the actual final reason of this is to have a folder well ordered of modules (in this case, functions). 好的,真正的最终原因是要使文件夹的模块顺序正确(在本例中为函数)。 The Folder structure should be something like this: 文件夹结构应如下所示:

  • /app.js /app.js
  • /node_modules/ / node_modules /
    • /mainfunction.js /mainfunction.js
    • /functions/ /职能/
      • /func1.js /func1.js
      • /func2.js /func2.js
      • /func3.js /func3.js
      • /func4.js /func4.js
      • /... / ...

mainfunction.js: mainfunction.js:

exports.func1 = require("./functions/func1");
exports.func2 = require("./functions/func2");
exports.func3 = require("./functions/func3");
exports.func4 = require("./functions/func4");

So, from app.js or any other part of the app i can use these functions just implementing mainfunction.js like this: 因此,从app.js或应用程序的任何其他部分,我可以仅通过实现mainfunction.js来使用这些功能,如下所示:

var f = require("mainfunction"); //And also require the others cores module 'path, fs,..."
//then you can call the module directly.
f.func1(input)
//Or store it in a variable
var area = (new) f.func3(width)  //sometimes **new** is needed when there is no input.

This info is avaliable in Node.js API . 该信息在Node.js API中可用。

The problems start now. 问题从现在开始。

Some modules require others to work, there are many relationships like these examples: 有些模块需要其他模块才能工作,这些示例之间有很多关系:

  1. Func2 require Func1 response Func2需要Func1响应
  2. Func3 require Func2 response and apart of it Func1 (so Func1 is required twice) Func3需要Func2响应,而Func1除外(因此Func1需要两次)
  3. Func4 require a core module (fs, path, ...) and others Functions. Func4需要一个核心模块(fs,path等)和其他功能。

Do i have to redeclare all the variables that every function (independent modules) needs? 我是否需要重新声明每个函数(独立模块)所需的所有变量?

example Func5.js: 示例Func5.js:

var answer;
var func2 = require("./func2");            //redeclare modules needed
var fs = require("fs");                    //redeclare core modules needed

module.exports = function(input) {         //do some stuff
    fs.exist("x.html", function (exist) {
        if(exist){
            answer = func2(input);
        }
     }
     return answer;
}

I dont want to redeclare the module necessary i just want to use "f.funcX(input)" to use a function in any scope, even inside the module. 我不想重新声明必要的模块,我只是想使用“ f.funcX(input)”来在任何范围内使用函数,即使在模块内部也是如此。 Is this possible? 这可能吗?

Any suggestion is well received. 任何建议都受到好评。 Thanks. 谢谢。

Another supposed option (Edited) 另一个假定选项(已编辑)

Ok, modules self-contained wins, but what about this? 好的,模块自成体系,但那又如何呢? (i do not even know if this works). (我什至不知道这是否可行)。

Same folders and files structure. 相同的文件夹和文件结构。 But mainfunction.js has: 但是mainfunction.js具有:

module.exports = {
    require("./functions/func1"),
    require("./functions/func2"),
    require("./functions/func3"),
    require("./functions/func4"),
    ...
}

And every Function file has the same but without redeclared modules, having "exports.functX" instead, and to call the others modules, uses the name of that function (module). 每个函数文件都具有相同但没有重新声明的模块,而是具有“ exports.functX”,并且要调用其他模块,请使用该函数(模块)的名称。

This might not work, it is just an idea. 这可能行不通,只是一个想法。

Func3 require Func2 response and apart of it Func1 (so Func1 is required twice) Func3需要Func2响应,而Func1除外(因此Func1需要两次)

As it turns out, Node's module loading system does not require Func1 twice in this situation. 事实证明,节点的模块加载系统不会在这种情况下需要FUNC1两次。 The process will cache Func1's exports and when required a second time, simply returns the original instantce of Func1. 该过程将缓存 Func1的导出,并在第二次需要时,仅返回Func1的原始实例。

Do I have to redeclare all the variables that every function (independent modules) needs? 我是否必须重新声明每个函数(独立模块)所需的所有变量?

You don't have to, but it's generally a good idea. 您不必这样做,但这通常是个好主意。 This is, by no means, a bad thing, since each module, in fact, should be independent of one another like this; 这绝不是一件坏事,因为实际上每个模块都应该像这样彼此独立。 if each module does something different, then it wouldn't make sense to have every function in every module in your project anyway. 如果每个模块都做不同的事情,那么无论如何在项目的每个模块中都没有每个功能。

Universal loading is technically possible, however. 但是,在技术上可以进行通用加载。 One possible configuration would be to load each module into the global namespace( process ), but in the words of Douglas Crockford , "Globals are evil." 一种可能的配置是将每个模块加载到全局名称空间( process )中,但是用Douglas Crockford的话来说,“全局是邪恶的”。 The module will be available anywhere in the Node process, even in modules that have nothing to do with yours. 该模块将在Node进程中的任何位置可用,即使在与您无关的模块中也是如此。 In essence, the require pattern that you see everywhere is, in fact, the best way to load dependencies in node. 本质上,到处可见的require模式实际上是在节点中加载依赖项的最佳方法。

Ok, modules self-contained wins, but what about this? 好的,模块自成体系,但那又如何呢? (i do not even know if this works). (我什至不知道这是否可行)。 [...] [...]

I think you mean this? 我想你是这个意思吗?

module.exports = {
    func1: require("./functions/func1"),
    func2: require("./functions/func2"),
    func3: require("./functions/func3"),
    func4: require("./functions/func4"),
    ...
}

This is generally a good pattern if you have a module that has a lot of independent submodules to expose to whatever required it; 如果您的模块有很多独立的子模块可以暴露给需要的任何东西,那么通常这是一个很好的模式。 however, it would not be possible to use such a pattern to allow a project-level namespace because Func1 would require mainfunction.js would require Func1, and so on forever. 但是,不可能使用这种模式来允许项目级名称空间,因为Func1需要mainfunction.js需要Func1,以此类推。 (This is known as a circular dependency.) (这被称为循环依赖。)

In short, don't worry about being repetitive in requiring modules. 简而言之,不必担心重复需要模块。 In the end, the standard pattern is not repetitive, because each module will have different requirements and dependencies, nor will it let a module run more than once, as explained above. 最后,标准模式不是重复的,因为每个模块都会有不同的要求和依赖性,也不会让一个模块运行多次,如上所述。

Good luck in your Node.js endeavors! 祝您在Node.js方面一切顺利!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM