morgan
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
basic-auth | 1.0.4 | 2.93 kB | MIT | prod | |
debug | 2.2.0 | 10.05 kB | MIT | prod | 1 1 1 1 |
depd | 1.0.1 | 8.33 kB | MIT | prod | |
on-finished | 2.3.0 | 4.45 kB | MIT | prod | |
on-headers | 1.0.2 | 3.15 kB | MIT | prod |
Morgan is a powerful HTTP request logger middleware for Node.js applications. It helps you monitor and analyze your app's incoming HTTP requests in real time. Morgan is exceptionally useful for debugging during development or even monitoring traffic patterns on production servers.
Morgan is easy to use within your Node.js application. Simply install it using npm:
npm install morgan
Then, require and use it inside your application file like so:
var express = require('express')
var morgan = require('morgan')
var app = express()
app.use(morgan('combined'))
app.get('/', function (req, res) {
res.send('hello, world!')
})
In this case, 'combined' is one of the predefined formats that Morgan offers. This will log requests in the Apache combined format. If you want to log using a custom format, you could use something like this:
app.use(morgan(':method :url :status :res[content-length] - :response-time ms'))
Morgan can also be used to log to a specific file stream:
var fs = require('fs')
var path = require('path')
var accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' })
app.use(morgan('combined', { stream: accessLogStream }))
Morgan's documentation, including more detail about formats, options, and custom tokens, can be found in the README file of its GitHub repository at https://github.com/expressjs/morgan. By consulting the official documentation, you can learn how to make the most of the tool and integrate it effectively into your Node.js applications.