Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Express.js · Module

morgan

Morgan is Node.js HTTP request-logger middleware for Express applications. Configure it with a predefined format, custom format string, or formatter function.

How it works

Calling morgan with a format and optional options returns middleware that records HTTP requests. The format can be a predefined name, a custom format string, or a function receiving tokens, the request, and the response; returning null or undefined from that function suppresses the entry.

Syntax

morgan(format, options)

Parameters

NameTypeDescription
formatstring | functionA predefined format name, custom format string, or function producing the log entry.
optionsobjectConfiguration options for the logger middleware.

Returns

A logger middleware function.

Key points

  • Morgan provides middleware dedicated to logging HTTP requests in Node.js.
  • The package is distributed as a Node.js module through npm.
  • The factory returns a logger middleware configured by the supplied format and options.
  • The format argument supports named built-in formats.
  • Formats may also be custom strings or functions that generate entries.
  • Custom formatters receive the available tokens together with the current request and response objects.

Common mistakes

  • A custom formatter must account for the three arguments Morgan supplies: tokens, the request, and the response.
  • A formatter should return the complete log-line string; returning another value does not produce the intended entry.
  • Returning undefined or null deliberately suppresses logging for that request.

Example: Log Express requests with a predefined format

import express from 'express';
import morgan from 'morgan';

const app = express();
app.use(morgan('combined'));
app.get('/', (_req, res) => res.send('ok'));
app.listen(3000);

Installs Morgan as application middleware so requests are logged using the predefined combined format.

When to use it

Inspect incoming requests while developing an Express service

Development

Record standardized request lines for operational logs

Production observability

Generate application-specific entries from request, response, and token data

Custom logging

Interview questions

QWhat is Morgan and what does its factory accept?

Morgan is HTTP request logger middleware for Node.js. Calling morgan(format, options) creates the middleware, and the format may be a predefined name, a format string, or a function.

Follow-up: How can a formatter selectively suppress entries? Return undefined or null for requests that should not be logged.

QHow does a custom Morgan format function work?

A formatter function receives tokens, req, and res. It should use those values to construct and return the desired log-line string.

Follow-up: What does returning no log-line mean? Morgan skips the entry when the formatter returns undefined or null.

QHow would you add Morgan to an Express application?

Morgan is installed from the npm registry and can be imported with ES modules or loaded with CommonJS. In an Express app, the returned logger is commonly passed to app.use().

Follow-up: Which predefined format can be used in the documented application example? The example uses combined.

  • Express middleware
  • app.use()
  • HTTP request
  • tokens
  • req
  • res

Source: morgan middleware | Express.js. Checked 2026-10-15.