-2

Possible Duplicate:
What is the purpose of NodeJS module.exports and how do you use it?

I have the following code:

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) {
....
};

what happened in this code. Second string says, that module.exports and app are the same object, right?

but in function(...) parts app pass as parameter and that code likes on "to object 'module' add method 'exports' and do it 2 times" I want to add some functions, which want to use inside each function (...), but can't because don't understand what happens in that constructions. Thanks

4

1 回答 1

1

为什么要分配 module.exports 三次?在您的代码中,module.exports 将首先等于调用 express 返回的内容。然后 module.exports 将等于您的函数(不是它返回的),并将接受 3 个参数。然后 module.exports 将等于您的最终函数(同样不是它返回的),带有 2 个参数。因此,在您的代码结束时 module.exports 将等于该最终功能。所以我看不出前两个任务需要什么。App 最后将等于 module.exports,因为 app 一直指向 module.exports。您是否希望将 app 作为参数传递给它并不重要,因为在将函数分配给 module.exports 之后,在上面的代码中,您实际上没有将 app 传递给函数。

我认为您要么在这里遗漏了代码,要么对您过去可能使用过的其他语言感到非常困惑。

如果您不清楚该语言,请查找 Douglas Crockford。

我希望这会有所帮助。

于 2013-01-17T14:34:27.523 回答