
Security News
How Enterprise Security Is Adapting to AI-Accelerated Threats
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.
exp-correlator
Advanced tools
Keep track of correlation id through a series of async operations. Either using a handler or an Express middleware. Uses async_hooks.
npm install exp-correlator
If an execution is started using either attachCorrelationIdHandler or occurs after the middleware is attached (when
the execution is part of the call chain of a Express request) any call to getId during that execution will return the
same correlation id. Example of use-cases may be to pass a correlation-id onto subsequent requests to other systems or
to be able to group log messages during the same request without passing on a correlation id between each function call.
The async handler will set the correlation id to the supplied or generate a new uuid v4 if not supplied.
const { attachCorrelationIdHandler, getId } = require("exp-correlator");
const logThis = async function (msg) {
const correlationId = getId();
console.log({correlationId, msg});
}
const f = async function () {
...
logThis("epic message");
logThis("epic message2");
...
};
await attachCorrelationIdHandler(f);
In the example above all the log messages produced by logThis can be grouped together by the correlation id without having to pass the correlation id as an argument every time.
The Express middleware will set the correlation id from the correlation-id or x-correlation-id header if available. Otherwise a
new uuid v4 will be generated. The middleware also assigns the correlation-id to the response, matching the header of incoming correlation-id if
it's available.
const { middleware, getId } = require("exp-correlator");
const express = require("express");
const app = express();
const logThis = async function (msg) {
const correlationId = getId();
console.log({correlationId, msg});
}
const callToExternalSystem () {
const correlationId = getId();
await ... // call to an external system setting correlationId as a header
}
app.use(middleware);
app.get("/", (req, res) => {
const correlationId = getId();
await callToExternalSystem();
logThis("epic message");
res.json({ correlationId });
...
});
In the example above the middleware set the correlation id based on the incoming header, it will then
be passed on when doing calls to callToExternalSystem and logThis without being explicitly passed.
To add correlationId when logging using pino do the following:
const { getId } = require("exp-correlator");
const pino = require("pino");
const logger = pino({mixin: () => {return { correlationId: getId() };}});
In the example above the correct correlation id will be added each time a log function is called when the express middleware or the async handler is used.
To add a correlation id when fetching using exp-fetch do the following:
const { getId } = require("exp-correlator");
const fetchBuilder = require("exp-fetch);
const fetch = fetchBuilder({ getCorrelationId: getId }).fetch;
await fetch("http://foo.bar");
In the example above the correlation id will be added to the outgoing requests headers as correlation-id.
Using bodyParser after using correlation middleware will cause the async local storage to be undefined.
This applies to any middleware that is derived from bodyParser as well (i.e. urlencoded).
const { middleware, getId } = require("exp-correlator");
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const logThis = async function (msg) {
const correlationId = getId(); // correlationId will be null here
console.log({correlationId, msg});
}
app.use(middleware);
// this won't work, bodyParser is used after correlation middleware for this route
app.post("/", bodyParser.json(), (req, res) => {
const correlationId = getId();
await callToExternalSystem();
logThis("epic message");
res.json({ correlationId });
...
});
// workaround add the correlation middleware after the bodyParser
app.post("/", bodyParser.json(), middleware, (req, res) => {
const correlationId = getId();
await callToExternalSystem();
logThis("epic message");
res.json({ correlationId });
...
});
Can be found here.
Released under the MIT license.
1.0.0
FAQs
Correlation-id handler and Express middleware
We found that exp-correlator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 11 open source maintainers collaborating on the project.
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.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.