CHAPTER 17

image

Domain and Express.js

The domain module is a core Node.js module (http://nodejs.org/api/domain.html) that aids developers in tracking and isolating errors, often a daunting task, through the use of domains. Think of domains as a smarter version of try/catch statements1.

Defining the Problem

To illustrate the hurdle that asynchronous code might introduce to error handling and debugging, look at this synchronous code that prints “Custom Error: Fail!” as we would expect:

try {  throw new Error('Fail!');} catch (e) {  console.log('Custom Error: ' + e.message);}

Now, let’s add asynchronous code in the form of the setTimeout() function

Get Pro Express.js now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.