Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
express-http-context2
Advanced tools
Modern request-scoped storage support for Express.js based on Asynchronous Local Storage.
Modern request-scoped storage support for Express.js, based on native Node.js Asynchronous Local Storage. It's a great place to store user state, claims from a JWT, request/correlation IDs, and any other request-scoped data.
This package is a drop-in replacement and has been inspired by the work done in express-http-context, but with ESM support, no dependencies, and only supporting modern versions of Node.js (+v14).
npm install express-http-context2
# or
yarn add express-http-context2
# or
pnpm add express-http-context2
Requirements
express-http-context2
is a middleware intended forexpress
, so although it is not explicitly declared as a dependency or peer dependency, it requiresexpress
to work, as well as@types/express
if you are using Typescript. The reason the dependency is not explicitly declared is that it could also eventually be used withfastify
or other Node.js HTTP servers.
Use the middleware immediately before the first middleware that needs to have access to the context. You won't have access to the context in any middleware "used" before this one.
const express = require('express')
const httpContext = require('express-http-context2')
const app = express()
// Use any third party middleware that does not need to access the context here, e.g.
// app.use(some3rdParty.middleware);
app.use(httpContext.middleware)
// All code from this point on will have access to the per-request context
Note that some popular middlewares (such as body-parser
, express-jwt
) can cause the context to be lost. To work around such problems, it is recommended that you use any third-party middleware that does NOT require the context BEFORE using this middleware.
Examples of setting values:
const httpContext = require('express-http-context2')
const { nanoid } = require('nanoid') // This is just an example, nanoid is not included in this lib
app.use((req, res, next) => {
// Get the user ID from wherever and save it for later use...
httpContext.set('userId', userId)
// Create a request ID to be able to trace/correlate everything that happens within the same request
httpContext.set('requestId', nanoid())
})
Get them from anywhere in your code:
var httpContext = require('express-http-context2')
function logError(error) {
const userId = httpContext.get('userId')
const requestId = httpContext.get('requestId')
console.error(error, { userId, requestId })
}
It is an Express.js middleware that is responsible for initializing the independent context for each request. The get
and set
calls will operate on a set of keys/values unique to those contexts.
import { middleware } from 'express-http-context2'
app.use(middleware)
Adds a value to the request context by key. If the key already exists, its value will be overwritten. No value will persist if the context has not yet been initialized.
key
a string key to store the variable byvalue
any value to store under the key for the later lookup.import { set } from 'express-http-context2'
set('user', { id: 'overwrittenUser', email: 'foo@bar.com' })
Gets a value from the request context by key.
Will return undefined
if the context has not yet been initialized for this request or if a value is not found for the specified key.
key
a string key to retrieve the stored value forimport { get } from 'express-http-context2'
const user = get('user')
Express HTTP Context 2 is released under the MIT license:
MIT License
Copyright (c) 2023 Alberto Varela Sánchez
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Modern request-scoped storage support for Express.js based on Asynchronous Local Storage.
The npm package express-http-context2 receives a total of 21,123 weekly downloads. As such, express-http-context2 popularity was classified as popular.
We found that express-http-context2 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
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.