
Security News
/Company News
Socket Is Sponsoring Composer and Packagist
Socket has joined the new Composer and Packagist sponsorship program as a launch sponsor, supporting the team that keeps PHP's package ecosystem secure.
request-ctxt
Advanced tools
Minimal Node.js library for managing per-request context and enhancing your logger automatically.
npm install request-context
import express from 'express';
import { expressMiddleware, logger } from 'request-context';
const app = express();
// Add the middleware early in your middleware chain
app.use(expressMiddleware({
// Optional configuration
requestIdHeader: 'x-request-id',
getUserId: req => req.user?.id,
getExtra: req => ({
// Add any extra context you want
ip: req.ip
})
}));
// Now all your logs will automatically include request context
app.get('/', (req, res) => {
logger.info('Hello world!'); // Will include requestId, path, userId, etc.
res.send('Hello!');
});
import fastify from 'fastify';
import { fastifyMiddleware, logger } from 'request-context';
const app = fastify();
// Register the middleware
app.addHook('onRequest', fastifyMiddleware({
// Optional configuration
requestIdHeader: 'x-request-id',
getUserId: request => request.user?.id,
getExtra: request => ({
// Add any extra context you want
ip: request.ip
})
}));
// Now all your logs will automatically include request context
app.get('/', (request, reply) => {
logger.info('Hello world!'); // Will include requestId, path, userId, etc.
reply.send('Hello!');
});
import http from 'http';
import { httpMiddleware, logger } from 'request-context';
const middleware = httpMiddleware({
// Optional configuration
requestIdHeader: 'x-request-id',
getUserId: req => req.user?.id,
getExtra: req => ({
// Add any extra context you want
ip: req.socket.remoteAddress
})
});
const server = http.createServer((req, res) => {
middleware(req, res, () => {
logger.info('Hello world!'); // Will include requestId, path, userId, etc.
res.end('Hello!');
});
});
server.listen(3000);
All middleware functions accept these options:
requestIdHeader (string): Header to use for request ID. Defaults to 'x-request-id'getUserId (function): Function to extract user ID from request. Defaults to req.user?.idgetExtra (function): Function to extract additional context from request. Defaults to empty objectimport pino from 'pino';
import { logger } from 'request-context';
// Configure with your preferred logger
logger.configure(pino());
// Now all logs will be handled by pino
logger.info('Hello world!');
The following context values are automatically added to every request:
requestId: Unique ID for each request (from header or generated)path: Request pathmethod: HTTP methoduserId: User ID (if available)Check out the examples directory for complete working examples using:
Each example demonstrates:
A publish script is included to help with releasing new versions. The script handles:
To use it:
# Make the script executable (one-time setup)
chmod +x publish.sh
# Publish a new version
./publish.sh <version_type>
# where version_type is one of:
# - major (for breaking changes)
# - minor (for new features)
# - patch (for bug fixes)
The script will:
MIT
FAQs
Minimal async request-scoped context manager for Node.js frameworks
The npm package request-ctxt receives a total of 12 weekly downloads. As such, request-ctxt popularity was classified as not popular.
We found that request-ctxt demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
/Company News
Socket has joined the new Composer and Packagist sponsorship program as a launch sponsor, supporting the team that keeps PHP's package ecosystem secure.

Research
/Security News
Benign-looking npm packages split malicious functionality across a dependency chain that deploys a cross-platform RAT targeting Alibaba developers.

Research
/Security News
Two Joyfill npm beta releases contain an import-time implant that uses blockchain transactions to retrieve a remote-access trojan.