I have a Node.js app that should run forever and am creating a single mongodb connection. However the connection keeps closing after a few seconds and crashing the app. I’ve stripped all the code back to just the create connection method. App code:
var mongoose = require('mongoose'),
db = mongoose.createConnection("mongodb://localhost:28017/mwa");
Then after a few seconds I get:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: connection closed
at [object Object].<anonymous> (/home/gavin/mwa/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:557:24)
at [object Object].emit (events.js:88:20)
at [object Object].<anonymous> (/home/gavin/mwa/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:109:15)
at [object Object].emit (events.js:70:17)
at Socket.<anonymous> (/home/gavin/mwa/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:427:12)
at Socket.emit (events.js:67:17)
at Array.0 (net.js:335:10)
at EventEmitter._tickCallback (node.js:192:40)
Check MongoDB’s log file to see if it’s hitting any issues that are making it kill your connection. As you mention, restarting MongoDB would be a good thing to try if there’s nothing in the log.