
Security News
The Nightmare Before Deployment
Season’s greetings from Socket, and here’s to a calm end of year: clean dependencies, boring pipelines, no surprises.
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-context
Now add our middleware to your ExpressJS app:
import expressContext from "express-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-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
Season’s greetings from Socket, and here’s to a calm end of year: clean dependencies, boring pipelines, no surprises.

Research
/Security News
Impostor NuGet package Tracer.Fody.NLog typosquats Tracer.Fody and its author, using homoglyph tricks, and exfiltrates Stratis wallet JSON/passwords to a Russian IP address.

Security News
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.