Skip to content

Commit 8c2387e

Browse files
author
troy
committed
update: add information to README.md
1 parent 26ca14c commit 8c2387e

File tree

4 files changed

+86
-7
lines changed

4 files changed

+86
-7
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
lib
2-
node_modules
2+
node_modules
3+
.vscode

README.md

+58-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,63 @@
11
# HTTP Context NodeJS
2+
23
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+
313
## 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+
862
## How it works
963
### TODO

test/index.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('Http Context', () => {
2121
HttpContext.scope();
2222
HttpContext.set('test', id)
2323
setTimeout(() => {
24-
const test = HttpContext.get('test')
24+
const test = HttpContext.get('test');
2525
resolve(test)
2626
}, 100)
2727
})
@@ -30,4 +30,28 @@ describe('Http Context', () => {
3030
asyncCall(1).then(v => expect(v).to.equal(1))
3131
asyncCall(2).then(v => expect(v).to.equal(2))
3232
})
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+
3357
});

tslint.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
22
"extends": ["tslint:recommended", "tslint-config-prettier"]
3-
}
3+
}

0 commit comments

Comments
 (0)