-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (47 loc) · 1.34 KB
/
server.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
53
54
55
56
const http2 = require('node:http2');
const fs = require('fs');
const server = http2.createSecureServer({
key: fs.readFileSync('./server-key.pem'),
cert: fs.readFileSync('./server-cert.pem')
});
server.on('stream', (stream, headers) => {
stream.respond({
// 'content-type': 'application/x-ndjson',
'Access-Control-Allow-Origin': 'http://127.0.0.1:8080',
'Access-Control-Allow-Headers': 'Content-Type',
':status': 200
});
if (headers[':method'] === 'OPTIONS') {
stream.end();
return;
}
let count = 0;
let done = false;
stream.on('data', async (chunk) => {
for (const it of users) {
it.email = it.email.toUpperCase();
console.log('write: ', JSON.stringify(it));
stream.write(JSON.stringify(it) + '\n');
}
// count++;
// console.log(`Receive : ${chunk.toString()}`);
// const user = JSON.parse(chunk);
// user.email = user.email.toUpperCase();
// await wait(4000);
// stream.write(JSON.stringify(user) +'\n');
// console.log('Send :', JSON.stringify(user));
// count--;
// if (count === 0 && done) {
// stream.end();
// }
});
stream.on('end', async () => {
done = true;
});
});
server.on('error', (err) => {
console.error('Server error:', err);
});
server.listen(3000, () => {
console.log('HTTP/2 server running on https://localhost:3000');
});