pulasthibandara / global-request-context

Global request context for node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This library is unmaintained!

Global Request Context

A middleware that enables you 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

  1. Register the middleware
// server.js:express
const expressContext = require('global-request-context/lib/express-middleware');
...
app.use(expressContext);
// server.js:koa
const koaContext = require('global-request-context/lib/koa-middleware');
...
app.use(koaContext);
  1. Simply require the context in
// fancy-service.js
// this is the request context koa/express
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;
}
  1. Run the application with zone.js as a polyfill
# require zone.js at execution
node -r zone.js server.js

or

// require zone.js as the first thing that you require
require('zone.js');
...
// rest of your app code

About

Global request context for node.js

License:MIT License


Languages

Language:JavaScript 100.0%