
Product
Socket for Jira Is Now Available
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.
express-request-context
Advanced tools
A tiny addon to ExpressJS to help you pass data along with the Request and Response objects in your route handlers.
This is a Node.js package available through the npm registry.
Installation into your project is done with the npm install command:
npm install --save express-request-context
Now add our middleware to your ExpressJS app:
import expressContext from "express-request-context";
const app = express();
app.use(expressContext()); // This will enable the 'context' object for you.
In your route handler, you now have a context object available in which you can shove any object or data that gets transferred to the next route handler. For example, you can get some user data from your database and use it in your next route:
Router.use((req, res, next) => {
const userData = db.getUserByToken(req.headers.authorization);
req.context.userData = userData;
next();
});
Have you ever been building an ExpressJS backend and encoutered the problem that you want to pass some data along with the Request and Response objects to the next route handler? Right, there's no such thing as extending those objects and adding your own stuff to it (in Typescript).
Router.use((req, res, next) => {
const accessToken = req.headers.authorization.token.replace("Bearer ", "");
const userData = getUserDataFromAccessToken(accessToken);
/*
The userData object now contains some data about the user that we want to use else where to:
- Check permissions
- Check ownership of a resource
- Compare ids or passwords
- Etc.
And we also need the accessToken for future stuff.
But how to pass these things along to the next handler?
*/
req.user = userData; // !
res.token = accessToken // !
next();
});
This won't work when you're using Typescript, because it will throw the following error:
TSError: ⨯ Unable to compile TypeScript:
src/myRouteHandler.ts:124:9 - error TS2339: Property 'user' does not exist on type 'Request<ParamsDictionary>'.
124 req.user = userData;
~~~~
src/myRouteHandler.ts:124:9 - error TS2339: Property 'token' does not exist on type 'Request<ParamsDictionary>'.
125 req.token = token;
~~~~~
Cause the user and token property don't exist on the Request object.
This package adds a context object of type any to both the Request and Response objects of ExpressJS. You can then put whatever you like in there to include with the Request in the next route handler:
Router.use((req, res, next) => {
const accessToken = req.headers.authorization.token.replace("Bearer ", "");
const userData = getUserDataFromAccessToken(accessToken);
req.context.user = userData; // !
res.context.token = accessToken // !
next();
});
The only thing you need to do to get started is add the middleware in this package to your ExpressJS app:
import expressContext from "express-request-context";
const app = express();
app.use(expressContext()); // This will enable the 'context' object for you.
Happy hacking!
Feel free to open an issue or a pull request in the repo on GitHub!
FAQs
Enable contexts in express Request and Response objects
We found that express-request-context 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.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.