
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
express-http-context
Advanced tools
The express-http-context package provides a way to manage and access context data across the lifecycle of an HTTP request in an Express application. This is particularly useful for scenarios where you need to share data between middleware and route handlers without explicitly passing it around.
Set and Get Context Data
This feature allows you to set and get context data within the lifecycle of an HTTP request. In this example, user data is set in the context in a middleware and then accessed in a route handler.
const express = require('express');
const httpContext = require('express-http-context');
const app = express();
app.use(httpContext.middleware);
app.use((req, res, next) => {
httpContext.set('user', { id: 1, name: 'John Doe' });
next();
});
app.get('/', (req, res) => {
const user = httpContext.get('user');
res.send(`Hello, ${user.name}`);
});
app.listen(3000, () => console.log('Server running on port 3000'));
Namespace Isolation
This feature ensures that context data is isolated per request by binding the request and response objects to the context namespace. This is useful for tracking request-specific data like request IDs.
const express = require('express');
const httpContext = require('express-http-context');
const app = express();
app.use(httpContext.middleware);
app.use((req, res, next) => {
httpContext.ns.bindEmitter(req);
httpContext.ns.bindEmitter(res);
next();
});
app.use((req, res, next) => {
httpContext.set('requestId', req.headers['x-request-id']);
next();
});
app.get('/', (req, res) => {
const requestId = httpContext.get('requestId');
res.send(`Request ID: ${requestId}`);
});
app.listen(3000, () => console.log('Server running on port 3000'));
cls-hooked is a continuation-local storage package that provides a way to maintain context across asynchronous operations. It is more general-purpose compared to express-http-context and can be used outside of Express applications. However, it requires more setup to integrate with Express.
async-local-storage is a built-in Node.js module that provides an API for maintaining context across asynchronous operations. It is similar to cls-hooked but is part of the Node.js core. It offers a more standardized approach but may require additional boilerplate to use effectively with Express.
Get and set request-scoped context anywhere. This package is an unopinionated, zero-dependency, Express-idiomatic implementation of Node AsyncLocalStorage. It's a great place to store user state, claims from a JWT, request/correlation IDs, and any other request-scoped data.
Install: npm i express-http-context
Use the context middleware before the first middleware or handler that needs to have access to the context.
import express from 'express';
import * as httpContext from 'express-http-context';
const app = express();
app.use(httpContext.middleware);
// All code from here on has access to the same context for each request
Set values based on the incoming request:
// Example authentication middleware
app.use(async (req, res, next) => {
try {
// Get user from data on request
const bearer = req.get('Authorization');
const user = await userService.getUser(bearer);
// Add user to the request-scoped context
httpContext.set('user', user);
return next();
} catch (err) {
return next(err);
}
});
Get them from code that doesn't have access to the express req
object:
import * as httpContext from 'express-http-context';
// Somewhere deep in the Todo Service
async function createTodoItem(title, content) {
// Get the user from the request-scoped context
const user = httpContext.get('user');
await db.insert({ title, content, userId: user.id });
}
npm install --save express-http-context@0
npm install --save express-http-context@1
Interesting in contributing? Take a look at the Contributing Guidlines
FAQs
Get and set request-scoped context anywhere
The npm package express-http-context receives a total of 236,979 weekly downloads. As such, express-http-context popularity was classified as popular.
We found that express-http-context demonstrated a healthy version release cadence and project activity because the last version was released less than 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
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.