Dr.YSG Dr.YSG - 1 year ago 92
Node.js Question

NodeJS Domains and expressjs

Newbie to ExpressJS question:

I am planning on creating a fairly simple (but large) ExpressJS project that responds to REST requests and does read-only DB responses. I have read up about NodeJS domains for capturing errors, but it is not clear to me that I need that for this project. My testing seems to indicate that even in the worst uncaught throws and crashes, only the individual REST request fails, and not the whole server.

Am I correct? NodeJS Domain API: http://nodejs.org/api/domain.html#domain_class_domain

(Obviously the reason I ask is that domain functionality is hard to implement after-the-fact at the end of the project).

This article left me with cold feelings to the idea of NODEJS for Express if it is not needed: (I don't complicate my code if not needed).

Node.js - Domain per Express request, inside another domain

Answer Source

Getting domains working is a lot less work with this module: https://github.com/brianc/node-domain-middleware.

Other than that, while it's true that only an individual request may fail, the errors can sometimes leave your server in a bad state (leaked resources are one example), and it's considered good form to restart the server at that point. If Express's built-in error handler takes care of that, you won't be able to shut down your server yourself. Thus, one reason why it's nice to have the domain, so that you can have a nice point in your code to capture that. The above middleware does a lot in pulling that error-handling code out of your main flow and keeping it clean.

Combined with forky, things get even better.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download