如何使用Node.js管理服务器端多个JS文件

时间:2021-10-27 15:56:31

I'm working on a project with Node.js and the server-side code is becoming large enough that I would like to split it off into multiple files. It appears this has been done client-side for ages, development is done by inserting a script tag for each file and only for distribution is something like "Make" used to put everything together. I realize there's no point in concatting all the server-side code so I'm not asking how to do that. The closest thing I can find to use is require(), however it doesn't behave quite like script does in the browser in that require'd files do not share a common namespace.

我正在和Node一起做一个项目。js和服务器端代码已经足够大了,我想把它分割成多个文件。这似乎已经在客户端进行了很长一段时间了,开发是通过为每个文件插入一个脚本标记来完成的,而且只有对于发行版来说,“Make”之类的东西才能将所有东西放在一起。我意识到把所有服务器端代码合并到一起是没有意义的,所以我并不是问怎么做。我能找到的最接近的使用方法是require(),但是它的行为与浏览器中的脚本不太一样,因为在浏览器中需要d文件不共享公共名称空间。

Looking at some older Node.js projects, like Shooter, it appears this was once not the case, that or I'm missing something really simple in my code. My require'd files cannot access the global calling namespace at compile time nor run time. Is there any simple way around this or are we forced to make all our require'd JS files completely autonomous from the calling scope?

看看一些旧的节点。js项目,比如Shooter,这一次似乎不是这样,我在代码中漏掉了一些非常简单的东西。我的require d文件不能在编译时或运行时访问全局调用命名空间。有什么简单的方法可以解决这个问题吗?还是我们*让所有需要的JS文件完全独立于调用范围?

2 个解决方案

#1


54  

You do not want a common namespace because globals are evil. In node we define modules

您不希望使用公共名称空间,因为全局变量是有害的。在节点中我们定义模块

// someThings.js

(function() {
    var someThings = ...;

    ...

    module.exports.getSomeThings = function() {
        return someThings();
    }

}());

// main.js

var things = require("someThings");
...
doSomething(things.getSomeThings());

You define a module and then expose a public API for your module by writing to exports.

您定义一个模块,然后通过写入到export来为模块公开一个公共API。

The best way to handle this is dependency injection. Your module exposes an init function and you pass an object hash of dependencies into your module.

处理此问题的最佳方法是依赖注入。模块公开一个init函数,并将依赖项的对象散列传递到模块中。

If you really insist on accessing global scope then you can access that through global. Every file can write and read to the global object. Again you do not want to use globals.

如果您真的坚持访问全局范围,那么您可以通过全局访问。每个文件都可以写入和读取到全局对象。同样,您不希望使用全局变量。

#2


32  

re @Raynos answer, if the module file is next to the file that includes it, it should be

如果模块文件在包含它的文件旁边,那么它应该是

var things = require("./someThings");

If the module is published on, and installed through, npm, or explicitly put into the ./node_modules/ folder, then the

如果模块发布在./node_modules/文件夹中,并通过npm进行安装,或者显式地放入

var things = require("someThings");

is correct.

是正确的。

#1


54  

You do not want a common namespace because globals are evil. In node we define modules

您不希望使用公共名称空间,因为全局变量是有害的。在节点中我们定义模块

// someThings.js

(function() {
    var someThings = ...;

    ...

    module.exports.getSomeThings = function() {
        return someThings();
    }

}());

// main.js

var things = require("someThings");
...
doSomething(things.getSomeThings());

You define a module and then expose a public API for your module by writing to exports.

您定义一个模块,然后通过写入到export来为模块公开一个公共API。

The best way to handle this is dependency injection. Your module exposes an init function and you pass an object hash of dependencies into your module.

处理此问题的最佳方法是依赖注入。模块公开一个init函数,并将依赖项的对象散列传递到模块中。

If you really insist on accessing global scope then you can access that through global. Every file can write and read to the global object. Again you do not want to use globals.

如果您真的坚持访问全局范围,那么您可以通过全局访问。每个文件都可以写入和读取到全局对象。同样,您不希望使用全局变量。

#2


32  

re @Raynos answer, if the module file is next to the file that includes it, it should be

如果模块文件在包含它的文件旁边,那么它应该是

var things = require("./someThings");

If the module is published on, and installed through, npm, or explicitly put into the ./node_modules/ folder, then the

如果模块发布在./node_modules/文件夹中,并通过npm进行安装,或者显式地放入

var things = require("someThings");

is correct.

是正确的。