npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-http-context2

v1.0.0

Published

Modern request-scoped storage support for Express.js based on Asynchronous Local Storage.

Downloads

115,178

Readme

Express HTTP Context 2

Rate on Openbase Contributor Covenant MIT license Build status codecov

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).

Installation

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 for express, so although it is not explicitly declared as a dependency or peer dependency, it requires express 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 with fastify or other Node.js HTTP servers.

Configuration

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.

Usage

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

API

middleware

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.

Example

import { middleware } from 'express-http-context2'

app.use(middleware)

set

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.

Parameters

  • key a string key to store the variable by
  • value any value to store under the key for the later lookup.

Example

import { set } from 'express-http-context2'

set('user', { id: 'overwrittenUser', email: '[email protected]' })

get

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.

Parameters

  • key a string key to retrieve the stored value for

Example

import { get } from 'express-http-context2'

const user = get('user')

License

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.