How can I test if a mongoose connection is already open?
My Node.js server code was getting unwieldy and long, so I recently began
refactoring functions into separate .js files, bringing them in via
requires. eg:
//server.js
var queries = require('./routes/queries');
var authenticate = require('./routes/authenticate');
//queries.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
exports.SomeFunctionB = ...
//authenticate.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
exports.SomeFunctionA = ...
However, now whenever I run my code, Mongoose.connect spits out an error
about trying to open an unclosed connection (though the code runs fine,
regardless). I understand that this is because I'm calling
mongoose.connect in both JS files.
So the first question: is there a better way to handle my Mongoose
connection for both JS files? (other than using Mongoose.connect at the
top of each one)
And the followup: If not, how can I test whether or not I need to make a
mongoose connection?
No comments:
Post a Comment