Global Request Context
A middleware that enables to require()
request context.
Tired of passing the request context through functions? Need to get the current
user session in that deeply nested function. Or simply coming from PHP or .NET
and don't wanna do it the node.js way? This is the module for you?
But seriously why?
The easy answer is... just because you can!.
How to use
- Register the middleware
const expressContext = require('global-request-context/lib/express-middleware');
...
app.use(expressContext);
const koaContext = require('global-request-context/lib/koa-middleware');
...
app.use(koaContext);
- Simply require the context in
const requestContext = require('global-request-context');
function handleRequestExpress() {
const { req, res } = requestContext;
res.send(req.query.input);
}
function handleRequestKoa() {
const { request, response } = requestContext;
response.body = request.query.input;
}
- Run the application with zone.js as a polyfill
node -r zone.js server.js
or
require('zone.js');
...