Skip to content

Get and set request-scoped context anywhere, and use it in any framework.

Notifications You must be signed in to change notification settings

yort-feng/http-context-nodejs

Folders and files

NameName
Last commit message
Last commit date
May 25, 2020
Nov 26, 2020
Nov 26, 2020
May 25, 2020
May 25, 2020
Nov 26, 2020
May 8, 2021
May 25, 2020
May 25, 2020
Nov 26, 2020

Repository files navigation

HTTP Context NodeJS

Get and set request-scoped context anywhere, and use it in any framework.

Installation

It requires node v8.2.1 or higher for ES2015 and async_hooks support.

$ npm i http-context-nodejs

Quick start

How to use it in NestJS

Set it in the MainJS

...
import * as uuid from 'node-uuid';
import * as httpContext from 'http-context-nodejs'
...
    app.use((req: Request, res: Response, next: NextFunction) => {
        httpContext.scope();
        httpContext.set('traceId', uuid.v1());
        next();
    });
...

Use it in anywhere

import * as httpContext from 'http-context-nodejs'
...
    const traceId = httpContext.get('traceId');
...

How to use it in ExpressJS

Set it in the AppJS

...
import * as uuid from 'node-uuid';
import * as httpContext from 'http-context-nodejs';
...
    app.use((req, res, next) => {
        httpContext.scope();
        httpContext.set('traceId', uuid.v1());
        next();
    });
...

Use it in anywhere

import * as httpContext from 'http-context-nodejs'
...
    const traceId = httpContext.get('traceId');
...

How it works

TODO

About

Get and set request-scoped context anywhere, and use it in any framework.

Resources

Stars

Watchers

Forks

Packages

No packages published