What is MORGAN in Node.js ?

05 Aug, 2021
Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside of the browser. It is widely used in developing APIs and microservices from small to large companies. It is a great tool as this enables developers to use JavaScript both on the server and client-side. In this article, We will discuss the MORGAN in Nodejs.
Morgan: Morgan is an HTTP request level Middleware. It is a great tool that logs the requests along with some other information depending upon its configuration and the preset used. It proves to be very helpful while debugging and also if you want to create Log files.
Prerequisites: Basic understanding of Nodejs.
 
Creating Project and Module Installation:
Step 1: Create a new folder for a project using the following command:mkdir morgan
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 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 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
Step 4: Install required packages using the following command:
npm i express morgan
Project Structure: It will look like the following:
 
Example 1: Using 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. 
NodeJS-Questions
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.