Node.js module / export system: is it possible to export a module as a function

I want to do something like this in Dispatch.js

function handle(msg) {
    ....
}
exports = handle;

and this is in the calling index.js

var dispatch = require("./Dispatch);
dispatch("data");

Any ideas?

0
source share
4 answers

exports = handle

This creates a local variable with the name exports. This is different from overwriting module.exports.

module.exports = handle

This overwrites the export variable, which lives in the module area, then it will be read require.

The browser window["foo"]and foothe same, but the node module["foo"]and foobehave differently.

The local context of the variable region and moduleis not the same thing.

+4
source

do:

function handle(msg) {
    ....
}
module.exports = handle;

, .

+1

(exports vs module.exports vs exports.something) :

http://www.alistapart.com/articles/getoutbindingsituations

(exports = handle) - : , javascript:

exports = handle window.exports = handle ( node.js )

0

- , node :

function loadModule(module, exports) {

}

exports (exports = handle), . module.

, , , .

0

All Articles