Debugging Node.js Applications

Gergely Nemeth, RisingStack

@nthgergo

Common error codes

EADDRINUSE
ECONNRESET
EACCESS
ENOENT

Graceful shutdown

Graceful shutdown

why does it matter?

SIGINT
a user wishes to interrupt the process
SIGTERM
the system wishes to interrupt the process
process.on(‘SIGINT’, function () {

   server.close(function () { process.exit(0); });

})

Error handling

error first callbacks

fs.readFile('path-to', function (err, data {
  if (err) {
    //handle error
    return;
  }
}) 

custom errors

function CustomError(message, extra) { 
    this.name = this.constructor.name
    this.message = message
    this.extra = extra
}; 

require('util').inherits(CustomError, Error);

error handling in express

app.use(function(err, req, res, next) {
  console.error(err.stack);
  res.status(500).send('Something broke!');
});

uncaught exceptions

process.on('uncaughtException', function (err) {

   // do something

  process.exit(129)

})

I Promise You

Promise.resolve()
.then(() => {
  a
})
process.on('unhandledRejection', function (err) {

   // do something
})

Debug time

node debug index.js
  • c => continue with code execution

  • n => execute this line and go to next line

  • s => step into this function

  • o => finish function execution and step out

node inspector

https://slides.com/gergelyke | https://github.com/gergelyke/node-debug

Gergely Nemeth, RisingStack

@nthgergo

Thanks!

Debugging Node.js applicatons

By Gergely Nemeth

Debugging Node.js applicatons

  • 875