# node-request-context

> Node request context let you save any context per request and easily retrieve it

Latest version **1.0.5** (published 2018-08-26) · ISC license · 0 weekly downloads

## Install

```sh
npm install node-request-context
pnpm add node-request-context
yarn add node-request-context
bun add node-request-context
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2018-08-26 |
| First published | 2017-11-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 81 |
| Author | Guy Segev |
| Maintainers | guyguyon |
| Keywords | es6, node, js, async, hooks, request, context |

## Links

- npm: https://www.npmjs.com/package/node-request-context
- Repository: https://github.com/guyguyon/node-request-context
- Homepage: https://github.com/guyguyon/node-request-context#readme
- Issues: https://github.com/guyguyon/node-request-context/issues
- npm.io page: https://npm.io/package/node-request-context

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.5 (latest) — 2018-08-26
- 1.0.4 — 2017-11-23
- 1.0.3 — 2017-11-22
- 1.0.2 — 2017-11-17
- 1.0.1 — 2017-11-16
- 1.0.0 — 2017-11-15

## README

[![npm version](https://badge.fury.io/js/node-request-context.svg)](http://badge.fury.io/js/node-request-context/)
[![Build Status](https://secure.travis-ci.org/guyguyon/node-request-context.svg?branch=master)](http://travis-ci.org/guyguyon/node-request-context)
[![Known Vulnerabilities](https://snyk.io/test/github/guyguyon/node-request-context/badge.svg)](https://snyk.io/test/github/guyguyon/node-request-context)



# Node Request Context

Node request context let you save any context per request and easily retrieve it.
Read my blog post about [building the package on Medium](https://medium.com/@guysegev/async-hooks-a-whole-new-world-of-opportunities-a1a6daf1990a)

> Attention! This package is using the new [async hooks](https://nodejs.org/api/async_hooks.html) API, which is available from Node 8.9.0, but still experimental. It is strongly recommended NOT to use in production environments.  

## Install

```bash
npm install node-request-context
```

## Usage

Node request context behaves pretty similar to [continuation-local-storage](https://github.com/othiym23/node-continuation-local-storage).


```javascript
const http = require('http');
const uuid = require('uuid');

// Create a namespace
const { createNamespace } = require('node-request-context');
const namespace = createNamespace('myapp.mynamespace');


http.createServer(function (req, res) {	
  // Wrap your request with namespace.run
  namespace.run(() => {
    const transactionId = uuid.v1();

    // Set any variable using the set function
    namespace.set('tid', transactionId);

    someAsyncFunc.then((data) => {

      // Get your variable using namespace.get 
      console.log('Some message', { transactionId: namespace.get('tid') });
      res.end(data);
    }).catch((err) => {
      console.log(err.message, { transactionId: namespace.get('tid') });
    });
  });

}).listen(8079);
``` 

You can retrieve any context variable in any other file

```javascript
const { getNamespace } = require('node-request-context');
const namespace = getNamespace('myapp.mynamespace');

class A {
  foo() {
    const tid = namespace.get('tid')
  }
}
```

## Example

To run a working example

1. Clone this repo
2. Make sure node > 8.9.0 is installed
2. Run `npm run example`
3. Open another terminal tab and run `curl localhost:8080`

## Author

Guy Segev 

[Email](mailto:guyguyon@gmail.com)  |  [LinkedIn](https://www.linkedin.com/in/guyguyon)  

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

---
_Source: https://npm.io/package/node-request-context · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
