What is MORGAN in Node.js ?

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.



Contact Us