I am trying to get my MEAN application ready for production. The application was built on the Mean.js boilerplate. From my understanding, MEAN.js uses Forever.js to restart the application after an error (although documentation on preparing Mean.js for production is severely lacking); however, it appears the suggested way to handle the application crashing is using Node's Domains in conjunction with clusters. Here are a few references:
- This is from Node's webpage on the deprecated
uncaughtException
Event:
Note that uncaughtException is a very crude mechanism for exception handling.
Don't use it, use domains instead.
- Node.js domains : https://nodejs.org/api/domain.html
- \http://shapeshed.com/uncaught-exceptions-in-node/
- etc.
Although I have found many suggestions for using domains, I have yet to find one that tells me what needs to be done to incorporate domains in an application, especially one that has already been developed.
The Questions
What do I need to do to integrate node domains into a Mean.js application? From what I have gathered (from the Node.js domains webpage and here), you would go into
server.js
in the root of the Mean.js project and do something similar to this:var cluster = require('cluster'); var PORT = +process.env.PORT || 1337; if (cluster.isMaster) { //Fork the master as many times as required. cluster.fork(); cluster.fork(); cluster.on('disconnect', function(worker) { console.error('disconnect!'); cluster.fork(); }); } else { var domain = require('domain'); var d = domain.create(); d.on('error', function(er) { console.error('error', er.stack); try { // make sure we close down within 30 seconds var killtimer = setTimeout(function() { process.exit(1); }, 30000); // But don't keep the process open just for that! killtimer.unref(); // stop taking new requests. server.close(); // Let the master know we're dead. This will trigger a // 'disconnect' in the cluster master, and then it will fork // a new worker. cluster.worker.disconnect(); // try to send an error to the request that triggered the problem res.statusCode = 500; res.setHeader('content-type', 'text/plain'); res.end('Oops, there was a problem!\n'); } catch (er2) { // oh well, not much we can do at this point. console.error('Error sending 500!', er2.stack); } }); d.run(function() { //Place the current contents of server.js here. }); }
Do I need to wrap all of the backend controllers in
domain.run()
?