Learn how to debug Express applications using the debug module and Node.js debugging tools
Express uses the debug module internally to log information about route matches, middleware functions, application mode, and the request-response cycle.
# Only router logsDEBUG=express:router node app.js# Router and application logsDEBUG=express:router,express:application node app.js# All express logs plus your app logsDEBUG=express:*,myapp:* node app.js
The debug module comes with Express, but you can also install it separately:
npm install debug
2
Create debug instances
Create debug instances for different parts of your application:
const debug = require('debug')('myapp:server');const dbDebug = require('debug')('myapp:database');debug('Server starting on port 3000');dbDebug('Connected to database');
const morgan = require('morgan');// Use 'dev' format in developmentif (app.get('env') === 'development') { app.use(morgan('dev'));}// Use 'combined' format in productionif (app.get('env') === 'production') { app.use(morgan('combined'));}
Avoid logging sensitive data like passwords, tokens, or personal information.