简体   繁体   中英

Nodejs module exports usage

In a sample application I saw a module with a code like this:

exports = mongoose = require('mongoose')
mongoose.connect(config.db.uri)
exports = Schema = mongoose.Schema

Can someone explain what the above code means? After these three lines I can see that mongoose and Schema functions can be called from anywhere in the application but I can't get the logic behind this.

exports = mongoose = require('mongoose')

This creates a variable called moongoose and sets it to be equal to require('mongoose') .

mongoose.connect(config.db.uri)

This starts a connection with a database.

exports = Schema = mongoose.Schema

This makes the module export require('mongoose').Schema for whatever reason.

This could be more simply written as:

var mongoose = require('mongoose')
mongoose.connect(config.db.uri)
exports = Schema = mongoose.Schema

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM