node.js - Understanding mongoose connections in express.js -
i learning express.js using following project: https://github.com/scotch-io/easy-node-authentication/tree/linking
in server.js can see , understand following initiates connection database using url database.js:
var mongoose = require('mongoose'); var configdb = require('./config/database.js'); mongoose.connect(configdb.url);
/app/models/user.js contains following:
var mongoose = require('mongoose'); var userschema = mongoose.schema({ local : { email : string, password : string, }, ... } module.exports = mongoose.model('user', userschema);
finally /config/passport.js contains:
var user = require('../app/models/user');
i can see how passport.js obtabs model user.js failing understand how user.js aware of connection setup in server.js initiated object "mongoose" not exported?
what missing?
as can see in file index.js @ last line
var mongoose = module.exports = exports = new mongoose;
this mean, mongoosee export 1 instance (singleton) handler database operations. because first create connection in server.js
file, after that, included/require model have connection db server. app work on single object.
Comments
Post a Comment