Menu

  • Home
  • Javascript (JS)
  • Node JS
  • React JS
  • React Native
  • More
    • Electron JS
    • WordPress
    • About
    • Code Examples
  • Contact Us
  • Our Services
  • +237 673917344
  • info@alljavascript.net
  • New Content Weekly
Beyond JavaScript Beyond JavaScript
  • Home
  • Javascript (JS)
  • Node JS
  • React JS
  • React Native
  • More
    • Electron JS
    • WordPress
    • About
    • Code Examples
  • Contact Us
  • Our Services
How to log Node JS http requests using morgan
  • July 20, 2022
  • admin
  • 0 Comments
  • 172 Views
  • 0 Likes
  • Node JS

How to log Node JS http requests using morgan

Morgan is a third-party middleware or module used to log every http request information in details as customized by the developer. This is a very useful dev tool.
Morgan makes work lot more easier and flexible.
Before using Morgan, make sure Morgan has been installed or install via npm with the command:-

npm install --save morgan

Or as a dev dependency,

npm install --save-D morgan

We will implement the simplest use case of morgan to easily get started with morgan.

Syntax

morgan (format, options)
  • Format refers to the “way” in which the request info should be logged on the console. Morgan comes with predefined formats which helps developers to easily setup and use morgan.
  • The options argument is an object used to further customize the functioning of morgan.

Note that, each format has something special in the way it logs request.
We will use the “tiny” predefined format in our code below and view the output on our cmd console.
The output for the “tiny” predefined format is: -method -url -status code -response content length -response time.


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

app.use(morgan('tiny')) // or dev or combined or common or any predefined format

app.get('/',(req, res)=>{
    res.send('Home page')
})

app.get('/about',(req, res)=>{
    res.send('about page')
})

app.listen(3000,()=>{
    console.log('code running on port 3000');
})

Output

Morgan at work after 2 successful http GET requests (home and about routes).

Thanks for coding with me..

Hope you learnt something new. You can always drop your comments and share with other devs. Remember we will cover more real code and projects in the next tutorials.

Links: Morgan Web Page, NPM Website

Spread the love
Tags:
MorganNode JSNPM
Next PostHow to send Json response in express js
Related Posts
  • How to Make API Calls in Electron.js: A Step-by-Step Tutorial February 7, 2024
  • How to Open and Read an Excel File in Node.js: A Step-by-Step Tutorial February 7, 2024

Leave a Comment Cancel Comment

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to Make API Calls in Electron.js: A Step-by-Step Tutorial
  • How to Open and Read an Excel File in Node.js: A Step-by-Step Tutorial
  • Electron.js and Express.js backend: A Comprehensive Tutorial
  • How to Get Started with Electron.js: A Tutorial for Beginners
  • How to add Favicon in Express.js: A Step-by-Step Tutorial

Recent Comments

No comments to show.

Popular Posts

February 7, 2024 / Electron JS, Node JS
How to Make API Calls in Electron.js: A Step-by-Step Tutorial
February 7, 2024 / Javascript (JS), Node JS
How to Open and Read an Excel File in Node.js: A Step-by-Step Tutorial
February 7, 2024 / Electron JS, Javascript (JS)
Electron.js and Express.js backend: A Comprehensive Tutorial

Tags

Express Js Json Morgan Node JS NPM

Categories

  • Electron JS
  • Html, CSS
  • Javascript (JS)
  • MongoDB
  • Node JS
  • React Native

We are a community of developers aimed at teaching code and solving coding problems. We provide coding articles, opportunities, content, videos and tutorials to help developers around the world...

Our Services

  • Website Design
  • Domain and Web Hosting
  • Tutoring
  • Mobile Apps
  • Website Content Writing

Recent Posts

  • How to Make API Calls in Electron.j

    Feb 7, 2024

  • How to Open and Read an Excel File

    Feb 7, 2024

Useful Links

  • Home
  • Services
  • Blog #2
  • About Us
  • Contact Us
  • Request Tutorials
  • Jobs
  • Volunteer
  • Hire Us
  • Support
info@alljavascript.net Drop Us a Line
+237 673917344 Call Us Now
Africa Get Direction
Copyright 2022 alljavascript.net, All rights reserved.