Getting Started with Morgan

mtee

Margaret W.N

Posted on August 24, 2020

Getting Started with Morgan

Morgan is a middleware function for logging information about the http request/response in a server application.

Installation

$ npm install morgan
Enter fullscreen mode Exit fullscreen mode

Use

const morgan = require('morgan');

app.use(morgan('dev'));
Enter fullscreen mode Exit fullscreen mode

Arguments.

Morgan takes two arguments: format and options.
FORMATS
You can defined your own format string or use the predefined formats. I like to keep things simple while learning a new concept hence i opted for the predefined formats. Here are a few predefined formats.

  • tiny - logs out minimal information about the request. status.
app.use(morgan('tiny'));
Enter fullscreen mode Exit fullscreen mode
  • dev logs out concise output with color coded status.
app.use(morgan('dev'));
Enter fullscreen mode Exit fullscreen mode
  • combined logs out Standard Apache combined log output (A lot of information that you probably don't care about).
app.use(morgan('combined'));
Enter fullscreen mode Exit fullscreen mode

For more formats refer to the documentation.

OPTIONS
Valid option (objects) accepted by morgan.

  • immediate - information is logged on request instead of on response
  • skip - determines if logging is skipped
  • stream - Output stream for writing log lines

More on this in the documentation

Day 39

πŸ’– πŸ’ͺ πŸ™… 🚩
mtee
Margaret W.N

Posted on August 24, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Day 23 of 100DaysOfCode
javascript Day 23 of 100DaysOfCode

October 3, 2024

Day21 of 100DaysOfCode
javascript Day21 of 100DaysOfCode

August 22, 2024

Day22 of 100DaysOfCode
javascript Day22 of 100DaysOfCode

September 7, 2024

Day 2 of #100daysofMiva Coding Challenge
100daysofmiva Day 2 of #100daysofMiva Coding Challenge

August 22, 2024