【发布时间】:2013-01-01 02:23:28
【问题描述】:
可能重复:
What is the purpose of NodeJS module.exports and how do you use it?
我有以下代码:
var express = require('express');
var app = module.exports= express();
require('./config/environment.js')(app, express, __dirname);
require('./routes/default.js')(app, __dirname);
module.exports = function (app, express, dirname) {
....
};
module.exports = function (app, dirname) {
....
};
这段代码发生了什么。第二个字符串说,module.exports 和 app 是同一个对象,对吧?
但是在函数(...)部分应用程序作为参数传递,并且该代码喜欢“对对象'模块'添加方法'导出'并执行2次”我想添加一些想要在内部使用的函数每个函数(...),但不能,因为不了解该结构中发生了什么。 谢谢
【问题讨论】:
-
这一切都在一个文件中吗?我也很困惑为什么
module.exports在同一个文件中定义了三个不同的时间。如果这些陈述在单独的文件中,请更清楚地说明。 -
在 3 个单独的文件中。但为什么它是主要的,一个文件还是三个?
-
这有很大的不同,因为 node.js 模块是基于文件的。当您引用
require中的文件时,它会运行指定文件中的代码并返回该文件中module.exports的值。见What is the purpose of NodeJS module.exports and how do you use it? 和module.exports vs exports in nodeJS
标签: javascript node.js express