Debugging Express

Aditya kumar
Last Updated: May 13, 2022

Introduction

Internally, Express logs information about route matches, middleware functions in use, application mode, and the request-response cycle flow using the debug module.

What is Debug?

Debug is similar to console.log, however, unlike console.log, debug logs do not need to be commented out in production code. By default, logging is disabled, but it can be enabled conditionally by setting the DEBUG environment variable.

Set the DEBUG environment variable to express:* when launching your app to access all the internal logs utilized by Express.

$ DEBUG=express:* node index.js

Use the same command on Windows.

> set DEBUG=express:* & node index.js

Running this command on the express generator's default app produces 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 you make a request to the app, the logs indicated in the Express code will appear:

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

 

Set the value of DEBUG to express: router to only see logs from the router implementation. Similarly, set the value of DEBUG to express: application to only see logs from the application implementation, and so on.
 

Applications generated by express

The debug module is used by an express command-generated application, and its debug namespace is scoped to the application's name.

For example, if you used $ express sample-app to create the app, you can use the following command to activate the debug statements:

$ DEBUG=sample-app:* node ./bin/www

You can assign a comma-separated list of names to indicate more than one debug namespace:

$ DEBUG=http,mail,express:* node index.js

 

Environment Variables

When using Node.js, you may change the behavior of the debug logging by setting a few environment variables:

Name

Purpose

DEBUGSpecific debugging namespaces are enabled or disabled.
DEBUG_COLORSWhether or not colors should be used in debug output.
DEBUG_DEPTHDepth of object inspection.
DEBUG_FDTo write debug output, create a file descriptor.
DEBUG_SHOW_HIDDENShows hidden properties on objects that have been investigated.

 

Note that environment variables that begin with DEBUG_ are transformed into an Options object that can be utilized with %o/%O formatters. The whole list may be found in the util.inspect() documentation in Node.js.

 

Formatters

Printf-style formatting is used by Debug. The following formatters are officially supported:

Formatter

Representation

%OOn many lines, pretty-print an Object.
%oOn a single line, pretty-print an Object.
%sString.
%dthe number (both integer and float).
%jJSON. If the argument contains circular references, the string '[Circular]' is substituted.
%%(%) is a single percent symbol. This does not necessitate a debate.

Custom formatters

By extending the debug. formatters object, you can add custom formatters. If you wanted to add support for rendering a Buffer as hex with %h, for example, you could do something like this:

const createDebug = require('debug')
createDebug.formatters.h = (v) => {
  return v.toString('hex')
}

// …elsewhere
const debug = createDebug('foo')
debug('this is hex: %h', new Buffer('hello world'))
//   foo this is hex: 68656c6c6f20776f726c6421 +0ms

 

Checking if a debug target is enabled or not

You can check whether a debug instance is enabled after you've created it by looking at the enabled property:

const debug = require('debug')('http');

if (debug.enabled) {
  // do stuff...
}
 

You may also manually toggle this option to enable or disable the debug instance.

 

Browser Support

You can use browserify to create a browser-ready script, or you can utilize the browserify-as-a-service build if you don't want to do it yourself.

The enabling status of Debug is currently saved in localStorage. Consider the following scenario: you have two workers, worker: a and worker: b, and you want to debug them both. You can use localStorage.debug to enable this:

localStorage.debug = 'worker:*'

 

Then go back to the website and reload it.

a = debug('worker:a');
b = debug('worker:b');

setInterval(function(){
  a('doing some work');
}, 1000);

setInterval(function(){
  b('doing some work');
}, 1200)

 

Frequently Asked Questions

1. What is debugging express?

Express logs information about route matching, middleware functions, application mode, and other things using the Debug module.

Set the DEBUG environment variable to Express to access all internal logs utilized by Express while launching the app-

DEBUG = express:* node index.js 

 

2. In an Express.js app, how do you enable debugging?

Debugging in an Express.js app can be enabled in a variety of ways depending on the operating system.

On Linux, use the following command:

DEBUG=express:* node app.js DEBUG=express:* node app.js
 

On Windows, type the following command:

set DEBUG=*node app.js DEBUG=*node app.js DEBUG=*node app.js

Key takeaways

In this blog, we learned Debugging in the express and got to know that Simply supply the name of your module to the debug function, and it will return a decorated version of the console. You can use this error to pass debug statements. This allows you to activate debug output for certain areas of your module as well as the entire module.

A colour is assigned to each debug instance based on its namespace name. 

You can learn more about Express and Express router before directly diving into debugging.

Was this article helpful ?
0 upvotes

Comments

No comments yet

Be the first to share what you think