express-http-context
Advanced tools
Comparing version
{ | ||
"name": "express-http-context", | ||
"version": "2.0.0-rc.0", | ||
"version": "2.0.0-rc.1", | ||
"description": "Get and set request-scoped context anywhere", | ||
@@ -34,6 +34,7 @@ "main": "index.js", | ||
"funding": "https://github.com/skonves/express-http-context?sponsor=1", | ||
"dependencies": { | ||
"peerDependencies": { | ||
"@types/express": "^4.16.0" | ||
}, | ||
"devDependencies": { | ||
"@types/express": "^4.16.0", | ||
"express": "^4.21.2", | ||
@@ -40,0 +41,0 @@ "jest": "^29.7.0", |
[](https://github.com/skonves/express-http-context/actions/workflows/build.yml) | ||
[](https://coveralls.io/github/skonves/express-http-context) | ||
[](https://www.npmjs.com/package/express-http-context) | ||
[](https://www.npmjs.com/package/express-http-context) | ||
# Express HTTP Context | ||
Get and set request-scoped context anywhere. This is just an unopinionated, idiomatic ExpressJS implementation of [Node AsyncLocalStorage](https://nodejs.org/api/async_context.html#class-asynclocalstorage). It's a great place to store user state, claims from a JWT, request/correlation IDs, and any other request-scoped data. | ||
Get and set request-scoped context anywhere. This package is an unopinionated, zero-dependency, Express-idiomatic implementation of [Node AsyncLocalStorage](https://nodejs.org/api/async_context.html#class-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 install --save express-http-context` | ||
Install: `npm i express-http-context` | ||
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. | ||
Use the context middleware before the first middleware or handler that needs to have access to the context. | ||
Note that some popular middlewares (such as body-parser, express-jwt) may cause context to get lost. | ||
To workaround such issues, you are advised to use any third party middleware that does NOT need the context | ||
BEFORE you use this middleware. | ||
``` js | ||
var express = require('express'); | ||
var httpContext = require('express-http-context'); | ||
import express from 'express'; | ||
import * as httpContext from 'express-http-context'; | ||
var app = express(); | ||
// Use any third party middleware that does not need access to the context here, e.g. | ||
// app.use(some3rdParty.middleware); | ||
const app = express(); | ||
app.use(httpContext.middleware); | ||
// all code from here on has access to the same context for each request | ||
// All code from here on has access to the same context for each request | ||
``` | ||
@@ -34,12 +26,16 @@ | ||
``` js | ||
// Example authorization middleware | ||
app.use((req, res, next) => { | ||
userService.getUser(req.get('Authorization'), (err, result) => { | ||
if (err) { | ||
next(err); | ||
} else { | ||
httpContext.set('user', result.user) | ||
next(); | ||
} | ||
}); | ||
// 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); | ||
} | ||
}); | ||
@@ -51,8 +47,10 @@ ``` | ||
``` js | ||
var httpContext = require('express-http-context'); | ||
import * as httpContext from 'express-http-context'; | ||
// Somewhere deep in the Todo Service | ||
function createTodoItem(title, content, callback) { | ||
var user = httpContext.get('user'); | ||
db.insert({ title, content, userId: user.id }, callback); | ||
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 }); | ||
} | ||
@@ -66,11 +64,2 @@ ``` | ||
## Troubleshooting | ||
To avoid weird behavior with express: | ||
1. Make sure you require `express-http-context` in the first row of your app. Some popular packages use async which breaks CLS. | ||
For users of Node 10 | ||
1. Node 10.0.x - 10.3.x are not supported. V8 version 6.6 introduced a bug that breaks async_hooks during async/await. Node 10.4.x uses V8 v6.7 in which the bug is fixed. See: https://github.com/nodejs/node/issues/20274. | ||
See [Issue #4](https://github.com/skonves/express-http-context/issues/4) for more context. If you find any other weird behaviors, please feel free to open an issue. | ||
## Contributors | ||
@@ -77,0 +66,0 @@ * Steve Konves (@skonves) |
6561
-12.68%4
33.33%71
-13.41%- Removed