module.exports vs exports in Node.js

前端 未结 23 1682
自闭症患者
自闭症患者 2020-11-22 06:11

I\'ve found the following contract in a Node.js module:

module.exports = exports = nano = function database_module(cfg) {...}

I wonder what

23条回答
  •  独厮守ぢ
    2020-11-22 06:57

    Each file you create is a module. module is an object. It has property called exports : {} which is empty object by default.

    you can create functions/middlewares and add to this empty exports object such as exports.findById() => { ... } then require anywhere in your app and use...

    controllers/user.js

    exports.findById = () => {
        //  do something
    }
    

    require in routes.js to use:

    const {findyId} = './controllers/user'
    

提交回复
热议问题