🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

express-http-context

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-http-context

Get and set request-scoped context anywhere

2.0.1
latest
Source
npm
Version published
Weekly downloads
292K
8.38%
Maintainers
1
Weekly downloads
 
Created

What is express-http-context?

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.

What are express-http-context's main functionalities?

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'));

Other packages similar to express-http-context

Keywords

express

FAQs

Package last updated on 19 Apr 2025

Did you know?

Socket

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.

Install

Related posts