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

response-time

Package Overview
Dependencies
Maintainers
6
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

response-time

Response time for Node.js servers

  • 2.3.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
354K
decreased by-29.29%
Maintainers
6
Weekly downloads
 
Created

What is response-time?

The response-time npm package is a middleware for Node.js that records the response time for HTTP requests. It is typically used with Express.js to measure the time taken to handle requests and send responses, which can be useful for performance monitoring and optimization.

What are response-time's main functionalities?

Basic Usage

This code demonstrates the basic usage of the response-time middleware in an Express.js application. It measures the response time for each HTTP request and adds it to the 'X-Response-Time' header.

const express = require('express');
const responseTime = require('response-time');

const app = express();

// Add the response-time middleware
app.use(responseTime());

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

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

Custom Response Time Header

This code demonstrates how to use the response-time middleware with a custom header. Instead of the default 'X-Response-Time' header, it sets a custom header 'X-Custom-Response-Time' with the response time.

const express = require('express');
const responseTime = require('response-time');

const app = express();

// Add the response-time middleware with a custom header
app.use(responseTime((req, res, time) => {
  res.setHeader('X-Custom-Response-Time', `${time}ms`);
}));

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

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

Logging Response Time

This code demonstrates how to log the response time for each request using the response-time middleware. It logs the HTTP method, URL, and response time to the console.

const express = require('express');
const responseTime = require('response-time');

const app = express();

// Add the response-time middleware with a logging function
app.use(responseTime((req, res, time) => {
  console.log(`Response time for ${req.method} ${req.url}: ${time}ms`);
}));

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

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

Other packages similar to response-time

Keywords

FAQs

Package last updated on 07 Oct 2024

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