What do “module.exports” and “exports.methods” mean in NodeJS / Express?

前端 未结 5 509
谎友^
谎友^ 2020-12-02 05:24

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

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 05:58

    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;
    

提交回复
热议问题