-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-server.mjs
98 lines (81 loc) · 2.44 KB
/
node-server.mjs
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// https://nodejs.org/api/http.html
import * as http from 'node:http';
import * as url from 'node:url';
import { WebSocketServer } from 'ws';
const sleep = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
const httpServer = http.createServer((request, response) => {
const requestPath = url.parse(request.url).pathname;
// request events
// close
// connect
// continue
// finish
// information
// response
// socket
// timeout
// upgrade
// request.on('close', () => console.log('closed'));
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Headers', '*');
response.setHeader('Access-Control-Allow-Methods', 'GET, POST');
response.setHeader('Content-Type', 'application/json');
const data = `{ "data": "response from server ${new Date().toISOString()}" }`;
if (requestPath === '/') {
response.write(data);
response.end();
}
if (requestPath === '/long-request') {
sleep(3000).then(() => {
response.write(data);
response.end();
});
}
if (requestPath === '/validate-get') {
sleep(3000).then(() => {
response.write('{ "invalid": true, "message": "Error from server"}');
response.end();
});
}
if (requestPath === '/validate-post') {
let body = '';
request.on('data', (value) => {
body += value;
});
request.on('end', () => {
if (body?.length) {
const bodyObject = JSON.parse(body);
sleep(3000).then(() => {
response.write(`{ "invalid": true, "message": "Error from server: ${bodyObject.field}"}`);
response.end();
});
} else {
response.end();
}
});
}
});
httpServer.listen(5000);
console.log('Node.js web server at port 5000 is running...');
const webSocketServer = new WebSocketServer({ port: 5001 });
let webSockets = [];
webSocketServer.on('connection', (socket) => {
console.log('connection opened');
webSockets.push(socket);
socket.on('message', (value) => {
console.log(JSON.parse(value));
sleep(3000).then(() => {
webSockets.forEach(s => {
console.log('sending message');
s.send(JSON.stringify({ data: 'message from backend', date: new Date() }));
});
});
});
socket.on('close', () => {
webSockets = webSockets.filter(s => s !== socket);
console.log('connection closed');
});
});
console.log('Node.js web socket server at port 5001 is running...');