My Node.js server code was getting unwieldy and long, so I recently began refactoring functions into separate .js files and 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. I understand that this is because I'm calling mongoose.connect
in both JS files.
Since I know that Server.js
, the Node.js file that actually gets run, require s queries.js
before authenticate.js
, can I simply leave out the second mongoose.connect
?
Perhaps more specifically, is the var mongoose
in the queries
file the same reference as the var mongoose
in the authenticate
file?
And if not, how can I test whether or not I need to make that mongoose connection in the first place?