Properly debugging your Express.js application

Debugging your application is very important, but often you end up with a lot of console.logs which is not recommended for production, since they are asynchronous functions, also, you have a lot of limitations. Today I’m going to teach you the basics of  
“debug module”
to debug your express application.
In order to follow this guide you need to know how to create an express application, I assume you already have one to start using the debug module with it. If you want more information about why you should not use console.log, you can visit the
“best practice performance”
express guide. Based on this
express debugging guide
, Express uses the
module internally to log information about route matches, middleware functions that are in use, application mode, and the flow of the request-response cycle. Let’s start by showing you how to enable the express debugging, this example uses a default app generated by the
express generator
, you just need to start your application and set the DEBUG environment variable like this:
// Linux
$ DEBUG=express:* node index.js

// Windows
> set DEBUG=express:* & node index.js
Running this command on the default app generated by the
express generator
prints the following output:
$ DEBUG=express:* node ./bin/www
 express:router:route new / +0ms
 express:router:layer new / +1ms
 express:router:route get / +1ms
 express:router:layer new / +0ms
 express:router:route new / +1ms
 express:router:layer new / +0ms
 express:router:route get / +0ms
 express:router:layer new / +0ms
 express:application compile etag weak +1ms
 express:application compile query parser extended +0ms
 express:application compile trust proxy false +0ms
 express:application booting in development mode +1ms
 express:router use / query +0ms
 express:router:layer new / +0ms
 express:router use / expressInit +0ms
 express:router:layer new / +0ms
 express:router use / favicon +1ms
 express:router:layer new / +0ms
 express:router use / logger +0ms
 express:router:layer new / +0ms
 express:router use / jsonParser +0ms
 express:router:layer new / +1ms
 express:router use / urlencodedParser +0ms
 express:router:layer new / +0ms
 express:router use / cookieParser +0ms
 express:router:layer new / +0ms
 express:router use / stylus +90ms
 express:router:layer new / +0ms
 express:router use / serveStatic +0ms
 express:router:layer new / +0ms
 express:router use / router +0ms
 express:router:layer new / +1ms
 express:router use /users router +0ms
 express:router:layer new /users +0ms
 express:router use / <anonymous> +0ms
 express:router:layer new / +0ms
 express:router use / <anonymous> +0ms
 express:router:layer new / +0ms
 express:router use / <anonymous> +0ms
 express:router:layer new / +0ms
When a request is then made to the app, you will see the logs specified in the Express code:
express:router dispatching GET / +4h
 express:router query  : / +2ms
 express:router expressInit  : / +0ms
 express:router favicon  : / +0ms
 express:router logger  : / +1ms
 express:router jsonParser  : / +0ms
 express:router urlencodedParser  : / +1ms
 express:router cookieParser  : / +0ms
 express:router stylus  : / +0ms
 express:router serveStatic  : / +2ms
 express:router router  : / +2ms
 express:router dispatching GET / +1ms
 express:view lookup "index.pug" +338ms
 express:view stat "/projects/example/views/index.pug" +0ms
 express:view render "/projects/example/views/index.pug" +1ms
If you want to print custom debugs for your application you have to require the ‘debug’ module in your application, you can have different debuggers in your application, each one with a custom name, for example, you can require one debugger for every file of your application, this way it’s easier to see what’s going on and where, you can have multiple debuggers in a file if you need it, but you should give a prefix to your debugger’s name, so it can be easily activated in your configuration, let’s see an example:
// server.js
const app = require('./app');
const server = require('http').Server(app);
const debug = require('debug')('app:server');
const port = process.env.PORT || 3000;

server.listen(port, () => {
 debug(`Server running on port: ${server.address().port}`);
This is a simple server.js file to start your application, notice that I’ve used require(‘debug’)(‘app:server’) and put it on a const named debug, (‘debug’) is the debug module, and (‘app:server’) is the name of your debugger, this is the name that will show on your console, I like to use ‘app:’ prefix for every debugger of my application, and ‘server’ is just the name of the file, now if I want to see only my custom debuggers on my console i just have to start my application like this:
// Linux
$ DEBUG=app:* node server.js

// Windows
> set DEBUG=app:* & node server.js
Running this command on the console will start your app then prints the following output:
app:server Server running on port: 3000 +9ms
The debug module is pretty easy to use and configure, you just need to specify what you want in the DEBUG environment variable and separate it by comma, you can exclude some debuggers from your console as well by using the ‘-’ character, let’s see some examples
// This will print every debugger on your console. Notice that ‘*’ means anything

// This will print all but ‘express:*’ on your console.

// This will print only the debuggers that start with ‘app:’ and ‘express:’
You can make some scripts in your package.json to start the application with some debuggers:
// package.json

"scripts": {
   "start": "node server.js",
   "start:dev": "set DEBUG=* & set NODE_ENV=development & node server.js"
You should keep your start script method clean, with no debuggers for production, the start:dev script sets the debug to print everything then we set the NODE_ENV variable to ‘development’ and finally we start the app, this is for Windows, you don’t need ‘set’ either ‘&’ on Linux. You can use this script in your terminal like this:
> npm run start:dev
I tested the debug module on Heroku recently, and guess what? It work perfectly, Heroku uses the ‘start’ script of your package.json to start the application, but you could have some development apps on Heroku, to check the debuggers from Heroku’s console, just set the DEBUG config var on your Heroku app and you are done.
Then go to your app logs Heroku.  
Select web on your logs page from Heroku, and done!
We are finished here, this is just a basic example of what you can do with the debug module, you will notice that a lot of javascript frameworks are using it, and it works for frontend too, it is pretty simple and you have more control of what you want to see on your console, keep in touch for more post!




Contact Us

Adress: 66 W Flagler St, Miami, Florida
Phone: +1 (786) 991-3467

© 2020 Cobuild Lab All Rights Reserved.