Socket
Socket
Sign inDemoInstall

server-timing

Package Overview
Dependencies
65
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    server-timing

This module can add `ServerTiming` Header to http response, and be able to use express middleware


Version published
Weekly downloads
10K
decreased by-12.13%
Maintainers
1
Install size
83.0 kB
Created
Weekly downloads
 

Readme

Source

server-timing

Build Status Coverage Status

This module adds Server-Timing to response headers. Example is here and open chrome devtool network tab.

You can use this as a express module / basic http function.

Install

$ npm install server-timing -S

Usage

const express = require('express');
const serverTiming = require('server-timing');

const app = express();
app.use(serverTiming());

app.use((req, res, next) => {
  res.startTime('file', 'File IO metric');
  setTimeout(() => {
    res.endTime('file');
  }, 100);
  next();
});
app.use((req, res, next) => {
  // you can see test end time response
  res.startTime('test', 'forget to call endTime');
  next();
});
app.use((req, res, next) => {
  // All timings should be in milliseconds (s). See issue #9 (https://github.com/yosuke-furukawa/server-timing/issues/9).
  res.setMetric('db', 100.0, 'Database metric');
  res.setMetric('api', 200.0, 'HTTP/API metric');
  res.setMetric('cache', 300.0, 'cache metric');
  next();
});
app.use((req, res, next) => {
  res.send('hello');
});

Conditionally enabled

const express = require('express');
const serverTiming = require('server-timing');

const app = express();
app.use(serverTiming({
  // Only send metrics if query parameter `debug` is set to `true`
  enabled: (req, res) => req.query.debug === 'true'
}));

API

constructor(options)

  • options.name: string, default total, name for the timing item
  • options.description: string, default Total Response Time, explanation for the timing item
  • options.total: boolean, default true, add total response time
  • options.enabled: boolean | function, default true, enable server timing header. If a function is passed, it will be called with two arguments, request and response, and should return a boolean.
  • options.autoEnd: boolean, default true automatically endTime is called if timer is not finished.
  • options.precision: number, default +Infinity, number of decimals to use for timings.

Result

image

Keywords

FAQs

Last updated on 10 Dec 2022

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc