
Security News
Software Engineering Daily Podcast: Feross on AI, Open Source, and Supply Chain Risk
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.
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
The npm package express-request-context receives a total of 1,235 weekly downloads. As such, express-request-context popularity was classified as popular.
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.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.

Security News
GitHub has revoked npm classic tokens for publishing; maintainers must migrate, but OpenJS warns OIDC trusted publishing still has risky gaps for critical projects.

Security News
Rust’s crates.io team is advancing an RFC to add a Security tab that surfaces RustSec vulnerability and unsoundness advisories directly on crate pages.