-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

Community
  • 1
  • 1
Oleg Sh
  • 8,496
  • 17
  • 89
  • 159
  • 1
    Is this all in one file? I, too, am confused why `module.exports` is defined three separate times in the same file. If these statements are in separate files, please indicate that more clearly. – apsillers Jan 17 '13 at 14:13
  • in 3 separated files. But why is it major, one file or three? – Oleg Sh Jan 17 '13 at 14:21
  • 1
    It makes a huge difference because node.js modules are file-based. When you reference a file in `require`, it runs the code in the specified file and returns the value of `module.exports` in that file. See [What is the purpose of NodeJS module.exports and how do you use it?](http://stackoverflow.com/questions/5311334/what-is-the-purpose-of-nodejs-module-exports-and-how-do-you-use-it) and also [module.exports vs exports in nodeJS](http://stackoverflow.com/questions/7137397/module-exports-vs-exports-in-nodejs) – apsillers Jan 17 '13 at 14:26

1 Answers1

1

Why are you assigning module.exports three times? In your code module.exports will first become equal to what ever is returned by calling express. Then module.exports will become equal to your function (NOT what it returns) and will take 3 arguments. Then module.exports will be equal to your final function (again NOT what it returns) taking 2 arguments. Therefore by the end of your code module.exports will be equal to that final function. So I don't see what the need is for the first two assignments. App will be equal to module.exports at the end because app is pointing to module.exports the whole time. It doesn't matter that you want app to be passed as an argument to it because no where in the code above do you actually pass app into the function, after assigning the functions to module.exports. All you have done here is name a parameter "app".

I think you have either missed code out here or got very confused by other languages you may have used in the past.

Lookup Douglas Crockford if the language isn't clear to you.

I hope that helps.

Ryan Smith
  • 1,255
  • 2
  • 13
  • 16
  • As you suspect, the OP has omitted code. Specifically, the OP has felt out the fact that each `module.exports` assignment is in a separate file (see the OP's comment on the question). – apsillers Jan 17 '13 at 14:40