JSPM

express-ctx

0.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 9554
  • Score
    100M100P100Q128671F
  • License MIT

express context based on async_hooks without getting lost

Package Exports

  • express-ctx

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (express-ctx) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

express-ctx

npm Travis (.org) Coverage Status

express context based on async_hooks without getting lost context

Install

npm i express-ctx

Usage

const express = require('express');
const { middleware, setValue, getValue } = require('express-ctx');
const usersRepository = require('./usersRepository'); // some your services

const app = express();

app.use(middleware); // must be set before using setValue/getValue

app.use((req, res, next) => {
  usersRepository.findOne(req.session.id).then(user => {
    setValue('user', user);
    next();
  });
});

app.use((req, res, next) => {
  const user = getValue('user');
  res.json(user);
});

Other libs

As for now such libraries as express-http-context and express-cls-hooked do not bind req and res to namespace, so there could be troubles with middlewares based on emitting events. So this lib binds.