I just do the following in my app.js file:
var mongoose = require('mongoose');
mongoose.connect('mongodb://address_to_host:port/db_name');
modelSchema = require('./models/yourmodelname').YourModelName;
mongoose.model('YourModelName', modelSchema);
// TODO: write the mongoose.model(...) command for any other models you have.
At this point any file that needs access to that model can do:
var mongoose = require('mongoose');
YourModelName = mongoose.model('YourModelName');
And finally in your model, you can have the file written normally then export it at the bottom:
module.exports.YourModelName = YourModelName;
I don't know if this the best most awesome solution (just started wrapping my head around exporting modules about 2 days ago) but it does work. Maybe someone can comment if this is a good way to do it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…