https://github.com/iamolegga/express-ctx
https://github.com/iamolegga/express-ctx
Last synced: 7 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/iamolegga/express-ctx
- Owner: iamolegga
- License: mit
- Created: 2019-08-30T09:21:55.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-24T00:03:15.000Z (over 2 years ago)
- Last Synced: 2025-03-17T23:51:47.195Z (7 months ago)
- Language: JavaScript
- Size: 508 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# express-ctx
  [](https://coveralls.io/github/iamolegga/express-ctx?branch=master)
`express` context based on `async_hooks` without getting lost context
## Install
```sh
npm i express-ctx
```## Usage
```js
const express = require('express');
const { middleware, setValue, getValue } = require('express-ctx');
const usersRepository = require('./usersRepository'); // some your servicesconst 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.