[英]Understanding mongoose connections in express.js
I am learning express.js using the following project: https://github.com/scotch-io/easy-node-authentication/tree/linking 我正在使用以下项目学习express.js: https : //github.com/scotch-io/easy-node-authentication/tree/linking
In server.js I can see and understand the following initiates a connection to the database using the url from database.js: 在server.js中,我可以看到并理解以下内容使用database.js中的url发起到数据库的连接:
var mongoose = require('mongoose');
var configDB = require('./config/database.js');
mongoose.connect(configDB.url);
/app/models/user.js contains the following: /app/models/user.js包含以下内容:
var mongoose = require('mongoose');
var userSchema = mongoose.Schema({
local : {
email : String,
password : String,
},
...
}
module.exports = mongoose.model('User', userSchema);
Finally /config/passport.js contains: 最后,/ config / passport.js包含:
var User = require('../app/models/user');
I can see how passport.js obtabs the model from user.js however I am failing to understand how user.js is aware of the connection setup in server.js as the initiated object "mongoose" is not exported? 我可以看到passport.js如何从user.js破坏模型,但是由于未导出启动对象“猫鼬”,因此我无法理解user.js如何知道server.js中的连接设置?
What am I missing? 我想念什么?
as you can see in this file index.js at the last line 如您在文件index.js的最后一行中所见
var mongoose = module.exports = exports = new Mongoose;
this mean, Mongoosee will export only one instance (singleton) to handler database operations. 这意味着Mongoosee将仅导出一个实例(单例)到处理程序数据库操作。 because you first create connection in your
server.js
file, after that, any included/require model will have connection to your db server. 因为您首先在
server.js
文件中创建连接,所以此后,任何包含/需要的模型都将与您的数据库服务器建立连接。 all app will work on single object. 所有应用都可以在单个对象上运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.