“module.exports"做什么?和“exports.methods"在 NodeJS/Express 中是什么意思? [英] What do "module.exports" and "exports.methods" mean in NodeJS / Express?
问题描述
查看express<的随机源文件/code>
NodeJS
的框架,有两行代码我看不懂(这几行代码几乎是所有 NodeJS 文件的典型代码).
/*** 暴露 `Router` 构造函数.*/出口 = 模块.出口 = 路由器;
和
/*** 公开 HTTP 方法.*/var methods =exports.methods = require('./methods');
我知道第一段代码 允许文件中的其余函数暴露给 NodeJS 应用,但我不完全理解 它是如何工作的,或者该行中的代码是什么意思.
<块引用>exports
和 module.exports
到底是什么意思?
我相信第二段代码允许文件中的函数访问methods
,但同样,它究竟是如何做到这一点的.
基本上,这些神奇的词是什么:module
和 exports
?
更具体的:
module
是文件内的全局范围变量.
所以如果你调用 require("foo")
那么:
//foo.js控制台日志(这个 === 模块);//真的
它的作用与window
在浏览器中的作用相同.
还有另一个名为 global
的全局对象,你可以在任何你想要的文件中写入和读取它,但这涉及改变全局范围,这就是 EVIL>
exports
是一个存在于 module.exports
上的变量.它基本上是您在需要文件时导出的内容.
//foo.jsmodule.exports = 42;//main.jsconsole.log(require("foo") === 42);//真的
exports
本身存在一个小问题._global 范围 context+ 和 module
不 相同.(在浏览器中,全局范围上下文和 window
是相同的).
//foo.jsvar 出口 = {};//创建一个名为exports的新局部变量,并与//生活在 module.exports 上出口 = {};//和上面一样module.exports = {};//只是有效,因为它是正确的"导出//bar.js出口.foo = 42;//这不会创建一个新的导出变量,所以它只是工作
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
?
To be more specific:
module
is the global scope variable inside a file.
So if you call require("foo")
then :
// foo.js
console.log(this === module); // true
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 EVIL
exports
is a variable that lives on module.exports
. It's basically what you export when a file is required.
// foo.js
module.exports = 42;
// main.js
console.log(require("foo") === 42); // true
There is a minor problem with exports
on it's own. The _global scope context+ and module
are not the same. (In the browser the global scope context and window
are the same).
// foo.js
var exports = {}; // creates a new local variable called exports, and conflicts with
// living on module.exports
exports = {}; // does the same as above
module.exports = {}; // just works because its the "correct" exports
// bar.js
exports.foo = 42; // this does not create a new exports variable so it just works
这篇关于“module.exports"做什么?和“exports.methods"在 NodeJS/Express 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!