3gwebtrain 3gwebtrain - 2 months ago 23
Node.js Question

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
})


thanks in advance!

Answer

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
Comments