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

node.jsExpress

node.js Problem Overview


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

node.js Solutions


Solution 1 - node.js

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

Solution 2 - node.js

Morgan is basically a logger, on any requests being made,it generates logs automatically.

Solution 3 - node.js

Morgan: is another HTTP request logger middleware for Node.js. It simplifies the process of logging requests to your application. You might think of Morgan as a helper that collects logs from your server, such as your request logs. It saves developers time because they don’t have to manually create common logs. It standardizes and automatically creates request logs.

Morgan can operate standalone, but commonly it’s used in combination with Winston. Winston is able to transport logs to an external location, or query them when analyzing a problem.

Solution 4 - node.js

Morgan is a popular HTTP request middleware logger for Node.js and basically used as a logger. It can be used with node js' winston package to consolidate HTTP request data logs with other information.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question3gwebtrainView Question on Stackoverflow
Solution 1 - node.jsPeter LyonsView Answer on Stackoverflow
Solution 2 - node.jsKrishna GaneriwalView Answer on Stackoverflow
Solution 3 - node.jsElle KiumarsianView Answer on Stackoverflow
Solution 4 - node.jsRavi MalviyaView Answer on Stackoverflow