File tree 4 files changed +86
-7
lines changed
4 files changed +86
-7
lines changed Original file line number Diff line number Diff line change 1
1
lib
2
- node_modules
2
+ node_modules
3
+ .vscode
Original file line number Diff line number Diff line change 1
1
# HTTP Context NodeJS
2
+
2
3
Get and set request-scoped context anywhere, and use it in any framework.
4
+
5
+ ## Installation
6
+
7
+ It requires node v8.2.1 or higher for ES2015 and async_hooks support.
8
+
9
+ ``` bash
10
+ $ npm i http-context-nodejs
11
+ ```
12
+
3
13
## Quick start
4
- ### TODO
5
- ### Express
6
- ### Nest
7
- ### Koa
14
+ ### How to use it in [ NestJS] ( https://nestjs.com/ )
15
+
16
+ #### Set it in the MainJS
17
+ ``` js
18
+ ...
19
+ import * as uuid from ' node-uuid' ;
20
+ import * as httpContext from ' http-context-nodejs'
21
+ ...
22
+ // Use httpContext and set traceId to trace all the logs within a http request
23
+ app .use ((req : Request , res : Response , next : NextFunction ) => {
24
+ httpContext .scope ();
25
+ httpContext .set (' traceId' , uuid .v1 ());
26
+ next ();
27
+ })
28
+ ```
29
+
30
+ #### Use it in anywhere
31
+
32
+ ``` js
33
+ import * as httpContext from ' http-context-nodejs'
34
+ ...
35
+ const traceId = httpContext .get (' traceId' );
36
+ ```
37
+
38
+ ### How to use it in ExpressJS
39
+
40
+ #### Set it in the AppJS
41
+
42
+ ``` js
43
+ ...
44
+ import * as uuid from ' node-uuid' ;
45
+ import * as httpContext from ' http-context-nodejs' ;
46
+ ...
47
+ app .use ((req , res , next ) => {
48
+ httpContext .scope ();
49
+ httpContext .set (' traceId' , uuid .v1 ());
50
+ next ();
51
+ });
52
+
53
+ ```
54
+ ### Use it in anywhere
55
+
56
+ ``` js
57
+ import * as httpContext from ' http-context-nodejs'
58
+ ...
59
+ const traceId = httpContext .get (' traceId' );
60
+ ```
61
+
8
62
## How it works
9
63
### TODO
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ describe('Http Context', () => {
21
21
HttpContext . scope ( ) ;
22
22
HttpContext . set ( 'test' , id )
23
23
setTimeout ( ( ) => {
24
- const test = HttpContext . get ( 'test' )
24
+ const test = HttpContext . get ( 'test' ) ;
25
25
resolve ( test )
26
26
} , 100 )
27
27
} )
@@ -30,4 +30,28 @@ describe('Http Context', () => {
30
30
asyncCall ( 1 ) . then ( v => expect ( v ) . to . equal ( 1 ) )
31
31
asyncCall ( 2 ) . then ( v => expect ( v ) . to . equal ( 2 ) )
32
32
} )
33
+
34
+ it ( 'expect work at multiple async resources' , ( ) => {
35
+ HttpContext . scope ( ) ;
36
+ HttpContext . set ( 'id' , 1 ) ;
37
+ setTimeout ( ( ) => {
38
+ HttpContext . set ( 'id' , 2 ) ;
39
+ setImmediate ( ( ) => {
40
+ HttpContext . set ( 'name' , 'http-context' ) ;
41
+ process . nextTick ( ( ) => {
42
+ HttpContext . set ( 'text' , 'who am I' ) ;
43
+ Promise . resolve ( ) . then ( async ( ) => {
44
+ const id = HttpContext . get ( 'id' ) ;
45
+ const name = HttpContext . get ( 'name' ) ;
46
+ const text = HttpContext . get ( 'text' ) ;
47
+ expect ( id ) . to . equal ( 2 ) ;
48
+ expect ( name ) . to . equal ( 'http-context' ) ;
49
+ expect ( text ) . to . equal ( 'who am I' ) ;
50
+
51
+ } ) ;
52
+ } ) ;
53
+ } ) ;
54
+ } , 10 ) ;
55
+ } )
56
+
33
57
} ) ;
Original file line number Diff line number Diff line change 1
1
{
2
2
"extends" : [" tslint:recommended" , " tslint-config-prettier" ]
3
- }
3
+ }
You can’t perform that action at this time.
0 commit comments