[英]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.
在这三行之后,我可以看到mongoose和Schema函数可以从应用程序的任何地方调用,但我无法得到这背后的逻辑。
exports = mongoose = require('mongoose')
This creates a variable called moongoose
and sets it to be equal to require('mongoose')
. 这将创建一个名为
moongoose
的变量,并将其设置为等于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. 这使得模块导出
require('mongoose').Schema
无论出于何种原因。
This could be more simply written as: 这可以更简单地写为:
var mongoose = require('mongoose')
mongoose.connect(config.db.uri)
exports = Schema = mongoose.Schema
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.