npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-correlation-id

v3.0.1

Published

Express middleware to correlate requests across http calls

Downloads

86,266

Readme

Express correlation id

Express middleware to set a correlation id per route in express. The correlation id will be consistent across async calls within the handling of a request.

Compatibility

From v3 onwards this library requires node >=16. For older node versions use v2.x.

Installation

npm i express-correlation-id --save

Middleware usage example

All middleware and route handlers following the correlator() middleware will be within a single correlation scope. If the incoming request has a header called x-correlation-id then it's value will be used as the id for this request, otherwise the id will be a new uuid.

Note: the correlator middleware should be placed after any other middleware.

const correlator = require('express-correlation-id');
const express = require('express');

const app = express();
// app.use other middleware here
app.use(correlator());

app.get('/', (req, res) => {
  console.log('ID for this request is:', req.correlationId()); // id for this request
  console.log('ID for this request is:', correlator.getId()); // equal to above, not dependant on the req object
  res.end();
});

API

correlator([options])

Returns an express middleware that creates a correlation scope for all following middleware and route handlers. If the incoming request has a header with name x-correlation-id then it's value will be used as the id. The header name is configurable, see options below. To ensure the correlation id is available to other middleware, ensure that it's applied after them.

const app = express();
// app.use other middleware here
app.use(correlator());

options

Options to configure the correlator middleware.

header

Configures the name of the inbound header to check for a correlation id.

const app = express();
app.use(correlator({ header: 'x-my-correlation-header-name' }));

correlator.getId()

Returns the id for the current request. If called outside of a request returns undefined. This function is useful if you don't want to pass the req object or correlation id from the handler to downstream code.

correlator.getId(); // Returns the current id or undefined

req.correlationId()

Returns the id for the current request. This function is added to the incoming req by the middleware.

req.correlationId(); // Returns the current id

correlator.setId(id)

Sets the id for the current request. If called outside of a request throws and error. Useful if you need to set the correlatiaon id and don't want to pass req object from the haandler to downstreama code.

correlator.setId('my-new-id');

req.setCorrelationId()

Sets the id for the current request. This function is added to the incoming req by the middleware.

req.setCorrelationId('my-new-id');

License

MIT