Mongoose.js creates multiple connections to MongoDB from one connect() call
Add this if statement to check only when mongoose disconnected, try to connect it
if (Mongoose.connection.readyState === 0)
mongoose.connect('localhost', 'test');
readyState contain these types :
0: disconnected
1: connected
2: connecting
3: disconnecting
That's because Mongoose uses a pool of 5 connections (by default) that are shared throughout your application. For best performance, it's best to just leave them open.
You can alter the default behavior via the options parameter to mongoose.connect
. For example:
mongoose.connect('localhost', 'test', { server: { poolSize: 3 }}); // Use 3 connections