Inspect incoming requests while developing an Express service
Development
Express.js · Module
Morgan is Node.js HTTP request-logger middleware for Express applications. Configure it with a predefined format, custom format string, or formatter function.
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.
morgan(format, options)| Name | Type | Description |
|---|---|---|
format | string | function | A predefined format name, custom format string, or function producing the log entry. |
options | object | Configuration options for the logger middleware. |
A logger middleware function.
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.
Development
Production observability
Custom logging
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.
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.
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 middlewareapp.use()HTTP requesttokensreqres