“module.exports”有什么作用?和“exports.methods” NodeJS / Express 中的意思是什么?

发布于 2024-11-09 05:06:07 字数 768 浏览 3 评论 0原文

查看 express 的随机源文件NodeJS 框架中,有两行代码我不明白(这两行代码是几乎所有 NodeJS 文件的典型代码)。

/**
 * Expose `Router` constructor.
 */

exports = module.exports = Router;

/**
 * Expose HTTP methods.
 */

var methods = exports.methods = require('./methods');

知道第一段代码 允许将文件中的其余函数公开给 NodeJS 应用程序,但我不太明白它是如何工作的,或者该行中的代码的含义。

exportsmodule.exports 的实际含义是什么?

我相信第二段代码允许文件中的函数访问方法,但同样,它到底是如何做到这一点的。

基本上,这些神奇的词是什么:moduleexports

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 and module.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

疯到世界奔溃 2024-11-16 05:06:07

更具体地说:

module 是文件内的全局范围变量。

因此,如果您调用 require("foo") 那么:

// foo.js
console.log(this === module); // true

它的行为方式与 window 在浏览器中的行为方式相同。

还有另一个名为 global 的全局对象,您可以在任何您想要的文件中写入和读取它,但这涉及到改变全局范围,这是 EVIL

exports< /code> 是一个位于 module.exports 上的变量。这基本上就是您在需要文件时导出的内容。

// foo.js
module.exports = 42;

// main.js
console.log(require("foo") === 42); // true

exports 本身存在一个小问题。 _global 作用域 context+ 和 module 相同。 (在浏览器中,全局范围上下文和窗口是相同的)。

// 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

了解有关导出的更多信息

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

Read more about exports

聚集的泪 2024-11-16 05:06:07

为了扩展 Raynos 的答案...

exports 基本上是 module.exports别名 - 我建议不要使用它。您可以通过在 module.exports 上设置来公开模块中的方法和属性,如下所示:

//file 'module1.js'
module.exports.foo = function () { return 'bar' }
module.exports.baz = 5

然后您可以在代码中访问它:

var module1 = require('module1')
console.log(module1.foo())
console.log(module1.baz)

您还可以 完全覆盖 module.exports,以便根据需要简单地提供单个对象:

//glorp.js
module.exports = function () {
  this.foo = function () { return 'bar' }
  this.baz = 5
  return this // need to return `this` object here
}

现在您已经有了一个很好的原型:

var g1 = new require('glorp')()
console.log(g1.foo())
console.log(g1.baz)

还有无数其他方法可以使用module.exports代码>和<代码>需要。请记住,require('foo') 始终返回相同的实例,即使您多次调用它也是如此。

注意

要使以下内容正常工作,

var g1 = new require('glorp')()
console.log(g1.foo())
console.log(g1.baz) 

必须在分配给 module.exports 的函数中返回 this。否则,您将收到 TypeError

console.log(g1.foo())
          ^
TypeError: Cannot read property 'foo' of undefined

To expand on Raynos's answer...

exports is basically an alias for module.exports - I recommend just not using it. You can expose methods and properties from a module by setting them on module.exports, as follows:

//file 'module1.js'
module.exports.foo = function () { return 'bar' }
module.exports.baz = 5

Then you get access to it in your code:

var module1 = require('module1')
console.log(module1.foo())
console.log(module1.baz)

You can also override module.exports entirely to simply provide a single object upon require:

//glorp.js
module.exports = function () {
  this.foo = function () { return 'bar' }
  this.baz = 5
  return this // need to return `this` object here
}

Now you've got a nice prototype:

var g1 = new require('glorp')()
console.log(g1.foo())
console.log(g1.baz)

There are myriad other ways to play with module.exports and require. Just remember, require('foo') always returns the same instance even if you call it multiple times.

Note

For the following to work,

var g1 = new require('glorp')()
console.log(g1.foo())
console.log(g1.baz) 

this has to be returned in the function that is assigned to module.exports. Otherwise, you'll get a TypeError:

console.log(g1.foo())
          ^
TypeError: Cannot read property 'foo' of undefined
琉璃梦幻 2024-11-16 05:06:07

你可以在node.js源代码中找到最好的答案。
如果有人需要你的 js 模块,
您的脚本按节点变成一个函数,如下所示(请参阅 src/node.js)。

// require function does this..
(function (exports, require, module, __filename, __dirname) {
    ... your javascript contents...
});

节点将包装您的脚本。然后上面的脚本将按如下方式执行:

//module.js
var args = [self.exports, require, self, filename, dirname];
return compiledWrapper.apply(self.exports, args);

因此,在您的脚本中,

exports is just module.exports.

在您的脚本中,您可以向此导出对象添加一些内容(函数..)。
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).

// require function does this..
(function (exports, require, module, __filename, __dirname) {
    ... your javascript contents...
});

Node will wrap your script. Then above script will be executed as follows:

//module.js
var args = [self.exports, require, self, filename, dirname];
return compiledWrapper.apply(self.exports, args);

So in your script,

exports is just module.exports.

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.

指尖上的星空 2024-11-16 05:06:07

模块是一个对象,表示特定源文件想要公开公开的内容。您可以通过定义此对象来描述模块导出的内容,而不是使用类似于 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.

清旖 2024-11-16 05:06:07

模块的代码包装在 module.exports 中(该模块,可能由其他模块组成)。
构建模块的方法有很多,但这是一种非常常见的方法(也是我个人最喜欢的方法)。

// Dependencies
// const module = require('module');

// Module object
var foo = {}

// Internal property
foo._a = 'a';

// "Public" property
foo.b = 'b';

// Method
foo.fu = function() { return 'fu' };

// Export
module.exports = foo;

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).

// Dependencies
// const module = require('module');

// Module object
var foo = {}

// Internal property
foo._a = 'a';

// "Public" property
foo.b = 'b';

// Method
foo.fu = function() { return 'fu' };

// Export
module.exports = foo;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文