“module.exports”有什么作用?和“exports.methods” NodeJS / Express 中的意思是什么?
查看 express
的随机源文件在 NodeJS
框架中,有两行代码我不明白(这两行代码是几乎所有 NodeJS 文件的典型代码)。
/**
* Expose `Router` constructor.
*/
exports = module.exports = Router;
我
/**
* Expose HTTP methods.
*/
var methods = exports.methods = require('./methods');
知道第一段代码 允许将文件中的其余函数公开给 NodeJS 应用程序,但我不太明白它是如何工作的,或者该行中的代码的含义。
exports
和module.exports
的实际含义是什么?
我相信第二段代码允许文件中的函数访问方法,但同样,它到底是如何做到这一点的。
基本上,这些神奇的词是什么:module
和 exports
?
Looking at a random source file of the express
framework for NodeJS
, there are two lines of the code that I do not understand (these lines of code are typical of almost all NodeJS files).
/**
* Expose `Router` constructor.
*/
exports = module.exports = Router;
and
/**
* Expose HTTP methods.
*/
var methods = exports.methods = require('./methods');
I understand that the first piece of code allows the rest of the functions in the file to be exposed to the NodeJS app, but I don't understand exactly how it works, or what the code in the line means.
What do
exports
andmodule.exports
actually mean?
I believe the 2nd piece of code allows the functions in the file to access methods
, but again, how exactly does it do this.
Basically, what are these magic words: module
and exports
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
更具体地说:
module
是文件内的全局范围变量。因此,如果您调用
require("foo")
那么:它的行为方式与
window
在浏览器中的行为方式相同。还有另一个名为
global
的全局对象,您可以在任何您想要的文件中写入和读取它,但这涉及到改变全局范围,这是 EVILexports< /code> 是一个位于
module.exports
上的变量。这基本上就是您在需要文件时导出的内容。exports
本身存在一个小问题。 _global 作用域 context+ 和module
不相同。 (在浏览器中,全局范围上下文和窗口
是相同的)。了解有关导出的更多信息
To be more specific:
module
is the global scope variable inside a file.So if you call
require("foo")
then :It acts in the same way that
window
acts in the browser.There is also another global object called
global
which you can write and read from in any file you want, but that involves mutating global scope and this is EVILexports
is a variable that lives onmodule.exports
. It's basically what you export when a file is required.There is a minor problem with
exports
on it's own. The _global scope context+ andmodule
are not the same. (In the browser the global scope context andwindow
are the same).Read more about exports
为了扩展 Raynos 的答案...
exports
基本上是module.exports
的别名 - 我建议不要使用它。您可以通过在module.exports
上设置来公开模块中的方法和属性,如下所示:然后您可以在代码中访问它:
您还可以 完全覆盖
module.exports
,以便根据需要简单地提供单个对象:现在您已经有了一个很好的原型:
还有无数其他方法可以使用
module.exports
代码>和<代码>需要。请记住,require('foo')
始终返回相同的实例,即使您多次调用它也是如此。注意
要使以下内容正常工作,
必须在分配给
module.exports
的函数中返回this
。否则,您将收到TypeError
:To expand on Raynos's answer...
exports
is basically an alias formodule.exports
- I recommend just not using it. You can expose methods and properties from a module by setting them onmodule.exports
, as follows:Then you get access to it in your code:
You can also override
module.exports
entirely to simply provide a single object upon require:Now you've got a nice prototype:
There are myriad other ways to play with
module.exports
andrequire
. Just remember,require('foo')
always returns the same instance even if you call it multiple times.Note
For the following to work,
this
has to be returned in the function that is assigned tomodule.exports
. Otherwise, you'll get aTypeError
:你可以在node.js源代码中找到最好的答案。
如果有人需要你的 js 模块,
您的脚本按节点变成一个函数,如下所示(请参阅 src/node.js)。
节点将包装您的脚本。然后上面的脚本将按如下方式执行:
因此,在您的脚本中,
在您的脚本中,您可以向此导出对象添加一些内容(函数..)。
require 函数将返回这个对象。这是node.js的模块系统(commonJS规范)。
但要注意不要修改 module.exports。否则你现在的出口就没有意义了。
You can find the best answer in node.js source code.
If someone is requiring your js module,
your script turns into a function by node as follows (see src/node.js).
Node will wrap your script. Then above script will be executed as follows:
So in your script,
In your script, you can add something to this exports object (functions..).
require function will return this object. This is node.js's module system (commonJS specification).
But be careful not to modify module.exports. Otherwise your current exports will be meaningless.
模块是一个对象,表示特定源文件想要公开公开的内容。您可以通过定义此对象来描述模块导出的内容,而不是使用类似于 c/c++ 世界中的头文件的内容。然后,节点运行时使用该对象来确定您的模块的哪些内容是“公共”的。
它与从编译世界中的 dll 导出函数类似的概念。您必须明确定义外部世界可以访问哪些功能。这有助于封装并让您以干净的方式组织库。
module is an object that represents what that particular source file would like to publicly expose. Instead of having something akin to header files in the c/c++ world, you describe what the module exports by defining this object. the node runtime then uses this object to determine what about your module is 'public.'
its a similar concept to exporting functions from a dll in the compiled world. you have to define explicitly what functions can be accessed by the outside world. this helps with encapsulation and lets your organize your libraries in a clean way.
模块的代码包装在 module.exports 中(该模块,可能由其他模块组成)。
构建模块的方法有很多,但这是一种非常常见的方法(也是我个人最喜欢的方法)。
Module's code is wrapped in module.exports (The module, maybe composed by other module).
There are many ways to build a module, but this is one very common (and my personal favorite).