JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 31254
  • Score
    100M100P100Q149605F
  • License MIT

Express middleware to correlate requests across http calls

Package Exports

  • express-correlation-id

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (express-correlation-id) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Build Status Coverage Status Dependencies npm version

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.

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.

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

const app = express();
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()

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.

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

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