-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1-id.js
50 lines (44 loc) · 1.32 KB
/
1-id.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict';
const http = require('node:http');
const { AsyncLocalStorage } = require('node:async_hooks');
const application = {
nextRequestId: 0,
asyncLocalStorage: new AsyncLocalStorage(),
user: { name: 'Marcus', balance: 0 },
};
const routing = {
'/': '<h1>welcome to homepage</h1><hr>',
'/user': application.user,
'/user/name': () => application.user.name.toUpperCase(),
'/user/balance': () => application.user.balance,
'/api/method1': async (req, res) => {
const id = application.asyncLocalStorage.getStore();
console.log(`${id} ${req.method} ${req.url} ${res.statusCode}`);
application.asyncLocalStorage.exit(() => {
if (id) console.log({ id });
});
return { id, user: application.user };
},
};
const types = {
object: JSON.stringify,
string: (s) => s,
undefined: () => 'not found',
function: async (fn, req, res) => {
const result = await fn(req, res);
return JSON.stringify(result);
},
};
http.createServer((req, res) => {
const data = routing[req.url];
const type = typeof data;
const endpoint = types[type];
const id = application.nextRequestId++;
application.asyncLocalStorage.run(id, async () => {
const result = await endpoint(data, req, res);
res.end(result);
});
}).listen(8000);
setInterval(() => {
application.user.balance += 10;
}, 1000);