Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-http-context

Package Overview
Dependencies
Maintainers
1
Versions
17
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

  • 1.2.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
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

FAQs

Package last updated on 12 Jul 2020

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc