What is MORGAN in Node.js ?
Last Updated :
13 Jun, 2024
Morgan is a popular HTTP request logger middleware for Node.js. It simplifies the process of logging HTTP requests in a Node.js application by automatically generating logs for incoming requests. Morgan provides various logging formats and options, allowing developers to customize the logging output according to their requirements.
Here's a breakdown of key aspects of Morgan:
- Logging Middleware: Morgan functions as middleware in Node.js applications, enabling you to integrate it seamlessly into your HTTP request handling pipeline.
- Request Logging: Morgan logs details about incoming HTTP requests, including the request method, URL, status code, response time, and more.
- Customizable Logging Formats: Morgan offers different predefined logging formats such as
combined
, common
, dev
, short
, and tiny
. These formats determine the structure and content of the generated logs. Additionally, developers can create custom logging formats tailored to their specific needs. - Integration with Express.js: Morgan is commonly used with Express.js, a popular web framework for Node.js. Integrating Morgan with Express.js is straightforward, as it can be used as middleware directly in Express applications.
- HTTP Method Support: Morgan logs details for various HTTP methods such as GET, POST, PUT, DELETE, etc., providing comprehensive insights into the application's HTTP traffic.
- Enhanced Debugging: By logging HTTP requests, Morgan aids in debugging and troubleshooting issues in Node.js applications. Developers can analyze the logs to identify errors, performance bottlenecks, and security vulnerabilities.
Installation Steps
Step 1: Create a new folder for a project using the following command:
mkdir morgan
Step 2: Navigate to our folder using the following command:
cd morgan
Step 3: Initialize npm using the following command and server file:
npm init -y
touch index.js
Step 4: Install required packages using the following command:
npm i express morgan
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"express": "^4.19.2",
"morgan": "^1.10.0"
}
Example 1: Implementation to Use dev as a preset in morgan.
JavaScript
const express = require('express');
const logger = require('morgan');
const port = 3000;
const app = express();
app.use(logger('dev'));
app.get('/', (req, res) => {
res.send('<h1>Front Page</h1>');
});
app.listen(port, () => {
console.log(`Started at ${port}`);
});
Steps to run: Run the application using the following command.
node index.js
Output: To send the request, We use a browser, That request will be logged by our logger morgan.

Then we will see the following output in our console.
Information about the get request on the home route is logged with status code 200.Explanation: Basically in the above code, we set up morgan, and since it's a middleware, So we used the .use() method to tell express to use that as a middleware in our app. Other than that we have used 'dev' as a preset. Some other presets available are combined, common, short, tiny. Each preset returns different information.Â
Example 2: In this example tiny is used as a preset inside morgan instead of dev.Â
JavaScript
const express = require('express');
const logger = require('morgan');
const port = 3000;
const app = express();
app.use(logger('tiny'));
app.get('/', (req, res) => {
res.send('<h1>Front Page</h1>');
});
app.listen(port, () => {
console.log(`Started at ${port}`);
});
Steps to run: Run the application using the following command.
node index.js
Output: To send the request, We use a browser, That request will be logged by our logger morgan.

Then we will see the following output in our console.

Explanation: In this 304 code is there, the Reason is since it is a simple static webpage, So browser cached it and returned its previous instance instead of making a new request.Â
Conclusion
Morgan simplifies the process of logging HTTP requests in Node.js applications, providing developers with valuable insights into application traffic and aiding in debugging and monitoring efforts. Its customizable logging formats and seamless integration with Express.js make it a popular choice among Node.js developers for HTTP request logging.
Similar Reads
What is package.json in Node.js ?
In the world of Node.js development, package.json is a crucial file that serves as the heart of any Node.js project. It acts as a manifest that defines the projectâs metadata, dependencies, scripts, and more. This article will provide an in-depth look at what package.json is, why it's essential, and
4 min read
What is spawn in Node JS ?
Node JS is a cross-platform, open-source back-end JavaScript runtime environment that uses the V8 engine to execute JavaScript code outside of an internet browser. In this article, we will learn about the Spawn in NodeJs. PrerequisitesNodeJS fundamentalsAsynchronous ProgrammingChild ProcessesSpawn i
3 min read
What is Buffer in Node.js ?
In Node, Buffer is used to store and manage binary data. Pure JavaScript is great with Unicode-encoded strings, but it does not handle binary data very well. It is not problematic when we perform an operation on data at the browser level but at the time of dealing with TCP stream and performing a re
3 min read
What are Modules in Node.js ?
In Node.js Application, a Module can be considered as a block of code that provide a simple or complex functionality that can communicate with external application. Modules can be organized in a single file or a collection of multiple files/folders. Almost all programmers prefer modules because of t
5 min read
What are modules in Node JS ?
In NodeJS, modules are encapsulated units of code that can be reused across different parts of an application. Modules help organize code into smaller, manageable pieces, promote code reusability, and facilitate better maintainability and scalability of NodeJS applications. Types of Modules:Core Mod
2 min read
What is NODE_ENV in Node ?
In Node.js, NODE_ENV is an environment variable used to define the current environment in which the Node.js application is running. It plays a crucial role in configuring the behavior of the application based on different environments such as development, staging, and production. This article delves
4 min read
What is Node?
Node is a JavaScript runtime environment that enables the execution of code on the server side. It allows developers to execute JavaScript code outside of a web browser, enabling the development of scalable and efficient network applications. Table of Content What is Node?Steps to setup the Node App
3 min read
Logging in Node.js
Node.js is a JavaScript runtime that's built on Chromeâs V8 JavaScript engine and its run-time environment includes everything which we'd like to execute a program written in JavaScript. Logging is an essential part of understanding the complete application life cycle of the Node.js program. From st
2 min read
What is Express-rate-limit in Node.js ?
express-rate-limit is a middleware for Express.js applications that helps control the rate at which requests can be made to the server. It is particularly useful for preventing abuse by limiting the number of requests a client can make to your API within a specific time frame. This can help mitigate
3 min read
What is REST API in NodeJS?
NodeJS is an ideal choice for developers who aim to build fast and efficient web applications with RESTful APIs. It is widely adopted in web development due to its non-blocking, event-driven architecture, making it suitable for handling numerous simultaneous requests efficiently. But what makes Node
7 min read