Javascript 如何使用 Node.js 在服务器端管理多个 JS 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5697061/
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
How to manage multiple JS files server-side with Node.js
提问by Matt Molnar
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 开发一个项目,并且服务器端代码变得足够大,我想将其拆分为多个文件。看起来这已经在客户端完成了很长时间,开发是通过script
为每个文件插入一个标签来完成的,并且仅用于分发是用于将所有内容放在一起的“Make”之类的东西。我意识到连接所有服务器端代码没有意义,所以我不是在问如何做到这一点。我能找到的最接近的东西是require()
,但是它的行为不像script
在浏览器中那样,因为所需的文件不共享公共命名空间。
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?
看看一些较旧的 Node.js 项目,比如Shooter,似乎曾经不是这样,或者我在我的代码中遗漏了一些非常简单的东西。我需要的文件在编译时和运行时都无法访问全局调用命名空间。有没有什么简单的方法可以解决这个问题,或者我们是否被迫使所有必需的 JS 文件完全独立于调用范围?
回答by Raynos
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
.
您定义一个模块,然后通过写入exports
.
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.
如果您真的坚持访问全局范围,那么您可以通过global
. 每个文件都可以写入和读取global
对象。同样,您不想使用全局变量。
回答by michielbdejong
re @Raynos answer, if the module file is next to the file that includes it, it should be
重新@Raynos 回答,如果模块文件在包含它的文件旁边,它应该是
var things = require("./someThings");
If the module is published on, and installed through, npm, or explicitly put into the ./node_modules/
folder, then the
如果模块是通过 npm 发布并安装的,或者明确放入./node_modules/
文件夹中,则
var things = require("someThings");
is correct.
是正确的。