DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 967,611 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Margaret W.N
Margaret W.N

Posted on

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

Top comments (0)

Need a better mental model for async/await?

Check out this classic DEV post on the subject.

β­οΈπŸŽ€ JavaScript Visualized: Promises & Async/Await

async await