Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to ensure Node.js keeps running after MonogDB connection drops?
    primarykey
    data
    text
    <p>I have an error-handling middlware in Express that tries to catch all incoming errors:</p> <pre><code>app.use(function(err, req, res, next){ console.error(err.stack); res.status(500); res.render('500.jade'); }); </code></pre> <p>But for some reason whenever I close <code>mongod</code> process, my application crashes with the following stack trace:</p> <pre><code>Error: failed to connect to [localhost:27017] at null.&lt;anonymous&gt; (/&lt;hidden&gt;/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:540:74) at EventEmitter.emit (events.js:106:17) at null.&lt;anonymous&gt; (/&lt;hidden&gt;/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15) at EventEmitter.emit (events.js:98:17) at Socket.&lt;anonymous&gt; (/&lt;hidden&gt;/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:478:10) at Socket.EventEmitter.emit (events.js:95:17) at net.js:441:14 at process._tickCallback (node.js:415:13) Process finished with exit code 8 </code></pre> <p>I have tried using the following configuration, but it didn't help me:</p> <pre><code>var options = { server:{ auto_reconnect: true, poolSize: 10, socketOptions:{ keepAlive: 1 } }, db: { numberOfRetries: 10, retryMiliSeconds: 1000 } } mongoose.connect(config.db, options); </code></pre> <p>One might wonder "why would you want your application to be running if it's essentially no longer functioning without a database connection?". I don't want it to crash and restart itself. Supervisor and Forever modules seem to stop trying to reconnect after a certain number of tries, thus leaving your application hanging in a crashed state.</p> <p><strong>Ideally, when MongoDB crashes I would like to display a 500.jade error page to users, meanwhile server should keep trying to reconnect to the database every 10 seconds. Once reconnected, resume all normal operations.</strong></p> <p>EDIT: None of the solutions posted below have worked for me, with the exception of domains. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload