1
I’m starting now with Node and used Mongoose to make my connections to the database in Mongodb. I put 1 log in my application, for every time I open the connection it print in this log.
What happens to you: Every time someone calls my 1 URL, my Node application opens a connection to Mongodb, is that normal and correct? That is, if 1,000 access my site, will there be 1,000 open connections? Is there any way to improve that and avoid problems, or is that normal for this environment?
For each model of mine, I have 1 code of this type:
var mongoose = require('mongoose');
var database = require('../config/database');
var mongoOptions = { db: { safe: true }};
mongoOptions.user = database.user;
mongoOptions.pass = database.pass;
console.log('Running mongoose version %s', mongoose.version);
mongoose.connect(database.url, mongoOptions, function (err, res) {
if (err) {
console.log ('ERROR connecting to: ' + database.url + '. ' + err);
} else {
console.log ('Successfully connected to: ' + database.url);
}
});
var Schema = mongoose.Schema;
var citySchema = new Schema({
name: String,
url: String,
uf: String,
dtRequest: Date,
active: Boolean,
loc: {
type: [Number],
index: '2dsphere'
}
});
module.exports = mongoose.model('City', citySchema);
var db = mongoose.connection;
db.on('error', console.error);
process.on('SIGINT', function() {
db.close(function () {
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
});
You can put your code?
– Sergio
Updated with 1 of my entities/model.
– Marlon Maxwel