-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
49 lines (40 loc) · 1.09 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
const fs = require('fs');
const http = require('http');
const WebSocketServer = require('ws').Server;
const server = http
.createServer((req, res) => {
console.log(req.method, req.url);
if (req.method === 'POST' && req.url.startsWith('/upload')) {
const chunks = [];
req.on('data', chunk => chunks.push(chunk));
req.on('end', () => {
const data = Buffer.concat(chunks);
res.writeHead(200, { 'Content-Type': 'text/plain' });
if (req.url.split('=').pop() === String(data.length)) {
res.end('DONE');
} else {
res.end('');
}
});
} else if (req.method === 'GET' && req.url.startsWith('/no-content')) {
res.writeHead(204);
res.end();
} else {
res.writeHead(405, { 'Content-Type': 'text/plain' });
res.end('');
}
})
.listen(7232);
const wss = new WebSocketServer({
server,
});
wss.on('connection', ws => {
ws.on('message', d => {
if (d instanceof Buffer) {
ws.send('DONE');
} else {
ws.send('');
}
});
ws.send(fs.readFileSync('./font.ttf'));
});