Debugging applications

Now that we've prepared ourselves for being able to diagnose issues in production, let's also see the tools available in the Node and hapi ecosystem for debugging applications in our development environment.

Coming from a PHP background which had quite a mature debugger, I always found debugging Node applications a bit more difficult to debug due to JavaScript's asynchronous nature. Fortunately, development tooling is improving around this, with more and more development environments being shipped with integrated Node debugging tools. Let's take a quick look at some of the currently available tools as well as some of the hapi-specific tools for debugging your hapi applications.

The console

This is generally every JavaScript developer's go-to when they have an issue. While it works for smaller issues, I wouldn't recommend trying to use this for everything. When it comes to more complex issues, you will want to have some experience with the node debugger.

The Node debugger

I generally try to make this my go-to tool when debugging applications, as being skilled in debugging an application will make you a productive developer overall. As I mentioned earlier, many IDEs and code editors are coming with an in-built Node debugger, such as Microsoft's open-sourced Visual Studio Code editor (https://code.visualstudio.com/), or Webstorm (https://www.jetbrains.com/webstorm/) by JetBrains.

At the time of writing this book, I have started doing all my code editing through the Visual Studio Code editor due to its built-in debugger support. These debuggers let you create break points, or use the debugger keyword to stop the process at the current location so that you can step through the code, looking at all the variables in the scope, which makes it much easier to diagnose any problems or unexpected behavior you might have encountered.

The debug mode in hapi

When creating a server, adding the following:

const server = new Hapi.Server({ debug: { log: ['error'] } });

will configure the server to log a detailed stack trace to the console for any routes that return an internal server error 500 response. As specified in the hapi API documentation, this should only be used in a development environment; it's not intended for production.

Profiling heapdumps

Embracing the silly naming conventions of hapi modules is the poop (https://github.com/hapijs/poop) module. Despite the name, this can be a very useful module for diagnosing a crashing process. When registered to a hapi server, for any uncaught exceptions, poop will generate a process .heapsnapshot file, which we can later profile.

This can be useful for identifying things such as what memory was used and where within the application at the time of crash. However, these .heapsnapshot files are a science in themselves. I would recommend reading up on some tutorials like the following when trying to grasp the contents of a heapdump file:

http://addyosmani.com/blog/taming-the-unicorn-easing-javascript-memory-profiling-in-devtools/.

TV

TV (https://github.com/hapijs/tv) is an interactive debug console that can be used with hapi applications. It provides a simple web page for viewing all your server logs. Details on how to use TV can be found on the repo's README page.

Debugging summary

Debugging applications has been identified as an area of difficulty in Node, and I expect the tools available for debugging both development and production issues will increase over the coming year, as Node is increasingly adopted in enterprises.

My recommendation for the tools listed here is to be comfortable with all of them before you ever put an application near production. You don't want to discover that your logging was insufficient, or learn to profile your first heapdump in the middle of some downtime. Trust me!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.114.85