Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

morgan

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

morgan

HTTP request logger middleware for node.js

  • 1.10.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.1M
increased by2.75%
Maintainers
1
Weekly downloads
 
Created

What is morgan?

Morgan is a middleware for Node.js that enables HTTP request logging. It is commonly used with Express.js applications to log information about incoming requests, which can be helpful for debugging, monitoring, and analytics purposes.

What are morgan's main functionalities?

Logging HTTP requests

This code sets up an Express server and uses Morgan to log all incoming HTTP requests in the 'combined' Apache format.

const express = require('express');
const morgan = require('morgan');
const app = express();

app.use(morgan('combined'));

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Customizing log formats

This code demonstrates how to customize the log format to include specific details such as the HTTP method, URL, status code, content length, and response time.

const express = require('express');
const morgan = require('morgan');
const app = express();

app.use(morgan(':method :url :status :res[content-length] - :response-time ms'));

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Creating custom tokens

This code shows how to create a custom token 'id' that can be used in the log format. The token function returns a value from the request object, which is then logged.

const express = require('express');
const morgan = require('morgan');
const app = express();

morgan.token('id', function getId(req) {
  return req.id;
});

app.use(morgan(':id :method :url'));

app.get('/', (req, res) => {
  req.id = 'abc123';
  res.send('Hello World!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Writing logs to a file

This code snippet demonstrates how to configure Morgan to write logs to a file named 'access.log' instead of outputting to the console.

const fs = require('fs');
const express = require('express');
const morgan = require('morgan');
const path = require('path');
const app = express();

const accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' });

app.use(morgan('combined', { stream: accessLogStream }));

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Other packages similar to morgan

Keywords

FAQs

Package last updated on 20 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc