Morgan is a popular HTTP request logging middleware for Node.js applications. It’s named after Dexter, a TV show you should not watch until completion. Morgan helps developers to log HTTP requests and responses in a customizable and extensible way.
Description
Morgan’s latest version is 1.10.0, and it was last published 4 years ago. You can install Morgan in your project by running npm i morgan
. There are currently 8786 other projects in the npm registry using Morgan.
Why Use Morgan?
Morgan is used to log HTTP requests and responses in Node.js applications. It provides various predefined formats and allows customizing the log format using tokens. Morgan is lightweight, easy to use, and can be integrated with popular web frameworks like Express.js.
Example
Let’s create a simple Express.js application using Morgan to log requests in the combined format:
const express = require('express');
const morgan = require('morgan');
const app = express();
// Use Morgan to log requests in the combined format
app.use(morgan('combined'));
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
In this example, Morgan is used to log requests in the combined format, which is the standard Apache combined log format. The log entries will include the remote address, remote user, date and time, request method, URL, HTTP version, status code, content length, referrer, user agent, and response time.
Conclusion
Morgan is a powerful and flexible HTTP request logger middleware for Node.js applications. It provides various predefined formats and allows customizing the log format using tokens. Morgan is easy to use and can be integrated with popular web frameworks like Express.js. To learn more about Morgan, visit its NPM page or GitHub repository.