Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

How Promises Change Post‑Mortem Debugging in Node.js Servers

This article explains how using JavaScript Promises in a Node.js HTTP server affects error handling, crash behavior, and core‑dump generation, contrasting a traditional callback server with a Promise‑based one and offering practical debugging options for post‑mortem analysis.

Promises enable reuse of client‑side and server‑side code, but error handling and debugging differ between the two environments.

On the client we usually hide errors from users, while on the server we prefer a fail‑fast approach, quickly restarting the process and preserving information for post‑mortem analysis.

What limitations do Promises introduce for post‑mortem debugging?

Server without Promise

'use strict';
const Http = require('http');
const server = Http.createServer((req, res) => {
  let result = '';
  req.on('data', (data) => {
    result += data;
  });
  req.once('end', () => {
    const obj = JSON.parse(result);
    res.writeHead(200);
    res.end(obj.foo.bar);
  });
});
server.listen(8080, () => {
  console.log('listening at http://localhost:8080');
});

This simple server crashes when receiving a request such as:

curl -X POST http://localhost:8080 -d '{"Hi": "world"}'

The crash log shows a TypeError because obj.foo.bar is undefined:

$ node server.js
listening at http://localhost:8080
/server.js:14
    res.end(obj.foo.bar);
                 ^

TypeError: Cannot read property 'bar' of undefined
    at IncomingMessage.req.once (/server.js:14:20)
    ...

Running Node with the --abort-on-uncaught-exception flag forces a core dump on uncaught exceptions:

$ node --abort-on-uncaught-exception server.js
listening at http://localhost:8080
Uncaught TypeError: Cannot read property 'bar' of undefined
FROM
IncomingMessage.req.once (/server.js:14:20)
...
Illegal instruction: 4 (core dumped)
Note: If no core file appears, check ulimit -c ; if it is 0, set ulimit -c unlimited . On macOS core files are stored in /cores/ as core.${PID} .

Server with Promise

'use strict';
const Http = require('http');
const server = Http.createServer((req, res) => {
  const promise = new Promise((resolve, reject) => {
    let result = '';
    req.on('data', (data) => {
      result += data;
    });
    req.once('end', () => {
      const obj = JSON.parse(result);
      resolve(obj);
    });
  });

  promise.then((obj) => {
    res.writeHead(200);
    res.end(obj.foo.bar);
  }).catch((reason) => {
    res.writeHead(500);
    res.end();
  });
});
server.listen(8080, () => {
  console.log('listening at http://localhost:8080');
});

The Promise version catches request‑parsing errors and returns a 500 response. An extended version logs the error and aborts the process:

'use strict';
const Http = require('http');
const server = Http.createServer((req, res) => {
  const promise = new Promise((resolve, reject) => {
    let result = '';
    req.on('data', (data) => {
      result += data;
    });
    req.once('end', () => {
      const obj = JSON.parse(result);
      resolve(obj);
    });
  });

  promise.then((obj) => {
    res.writeHead(200);
    res.end(obj.foo.bar);
  }).catch((reason) => {
    res.writeHead(500);
    res.end();
    console.error(reason);
    process.abort();
  });
});
server.listen(8080, () => {
  console.log('listening at http://localhost:8080');
});

Using Promises changes how crashes are reported: instead of an uncaught exception causing a core dump, the server can handle the error gracefully or deliberately abort, producing a core dump only when process.abort() is invoked.

For the full discussion, see the original article at https://www.joyent.com/blog/post-mortem-debugging-and-promises.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Node.jsError HandlingPost-Mortem Debugging

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion