forked from uNetworking/uWebSockets.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBenchmarker.js
52 lines (49 loc) · 1.02 KB
/
Benchmarker.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
51
52
/* Non-SSL is simply App() */
require('uWebSockets.js')
.App()
.get('/', (res, req) => {
res.writeHeader('content-type', 'text/plain').end('Hi')
})
.get('/id/:id', (res, req) => {
res.writeHeader('content-type', 'text/plain')
.writeHeader('x-powered-by', 'benchmark')
.end(`${req.getParameter('id')} ${req.getQuery('name')}`)
})
.post('/json', (res, req) => {
readJson(
res,
(obj) => {
res.writeHeader('content-type', 'application/json').end(
JSON.stringify(obj)
)
},
() => {
res.end('Ok')
}
)
})
.listen(3000, (listenSocket) => {
if (listenSocket) {
console.log('Listening to port 3000')
}
})
function readJson(res, cb, err) {
let buffer
res.onData((ab, isLast) => {
let chunk = Buffer.from(ab)
if (isLast) {
if (buffer) {
cb(JSON.parse(Buffer.concat([buffer, chunk])))
} else {
cb(JSON.parse(chunk))
}
} else {
if (buffer) {
buffer = Buffer.concat([buffer, chunk])
} else {
buffer = Buffer.concat([chunk])
}
}
})
res.onAborted(err)
}