Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `morgan` module have to do with express apps?

In an express tutorial, the author was using the npm module morgan. What can morgan do for an express app? Could anyone help me understand this?

Got this by googling, but I do not understand anything here:

var express = require('express') var morgan  = require('morgan')  var app = express() app.use(morgan('combined')) morgan('combined')  morgan(':remote-addr :method :url')  morgan(function (tokens, req, res) {   return req.method + ' ' + req.url }) 
like image 229
3gwebtrain Avatar asked Aug 24 '14 05:08

3gwebtrain


People also ask

What is Morgan used for in Express?

morgan is a Node. js and Express middleware to log HTTP requests and errors, and simplifies the process.

What does the Express Module do?

Express is a minimal and extensible framework that provides a set of common utilities for building servers and web applications. It's built on top of Node. js' built-in http module, but helps us accomplish the tasks of routing and handling the Request/Response cycle.

Why do we use Morgan in node JS?

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.

Is Express a framework or module?

Express itself is a module, as are the middleware and database libraries that we use in our Express applications. The code below shows how we import a module by name, using the Express framework as an example.


1 Answers

Morgan is used for logging request details. However, the snippet in your question doesn't make sense because it's not actually a single coherent snippet top to bottom. It is a series of examples of the various types of options you can pass to morgan. In a real program, you would only need one of them. For example:

var express = require('express') var morgan  = require('morgan')  var app = express() //This tells express to log via morgan //and morgan to log in the "combined" pre-defined format app.use(morgan('combined')) //That's it. Everything in your snippet after this are just //other variations your might want to use 
like image 125
Peter Lyons Avatar answered Oct 07 '22 02:10

Peter Lyons