Launch Week Day 4: Introducing Data Exports.Learn More
Socket
Book a DemoSign 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

latest
Source
npmnpm
Version
2.0.1
Version published
Weekly downloads
330K
6.17%
Maintainers
1
Weekly downloads
 
Created
Source

build coveralls npm npm

Express HTTP Context

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.

How to use it

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

Legacy versions

  • For Node <7: npm install --save express-http-context@0
  • For Node >=8 <12: npm install --save express-http-context@1

Contributors

  • Steve Konves (@skonves)
  • Amiram Korach (@amiram)
  • Yoni Rabinovitch (@yonirab)
  • DontRelaX (@dontrelax)
  • William Durand (@willdurand)
  • Kristopher Morris (@beeduck)

Interesting in contributing? Take a look at the Contributing Guidlines

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