Open In App

What is the purpose of the morgan middleware in Express.js?

Morgan is a widely used middleware for logging HTTP requests. It simplifies the process of logging HTTP requests in your application by automatically capturing request information such as request method, URL, status code, response time, and more. Morgan logs this information to the console or a specified log file, making it useful for debugging, monitoring, and analyzing your application’s HTTP traffic.

Purpose of the morgan middleware in ExpressJS:

The morgan middleware in ExpressJS simplifies the process of logging HTTP requests, providing valuable insights into request processing and aiding in debugging and monitoring of your web application.



Syntax:

Install the following dependency in you application using the following command.

npm install express morgan

Below is the basic syntax:



const express = require("express");
const morgan = require("morgan");
const app = express();

// Use morgan middleware to log HTTP requests to the console
app.use(morgan('dev'));

//your applciation routes define here

app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Article Tags :