This repository was archived by the owner on Aug 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path4-multipart-form-data.ts
More file actions
60 lines (51 loc) · 1.42 KB
/
4-multipart-form-data.ts
File metadata and controls
60 lines (51 loc) · 1.42 KB
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
import { createServer, Server } from 'net';
const port: number = 9903;
// ---------------------------------------------
// server
// ---------------------------------------------
const form: string = `
<html>
<body>
<form action="http://localhost:${port}/upload" method="post" enctype="multipart/form-data">
<input type="file" name="my-file">
<input type="submit">
</form>
</body>
</html>
`;
const server: Server = createServer((socket) => {
console.log(`[server] connected client: ${JSON.stringify(socket.address())}`);
socket.on('data', data => {
console.log(`[server] received data from client: ${socket.bytesRead}`);
const raw: string = data.toString();
console.log(raw);
if (/^GET \/ /i.test(raw)) {
const response: string = [
'HTTP/1.1 200 OK',
'Content-Type: text/html',
'Status: 200',
'',
'',
form,
'',
'',
].join('\r\n');
socket.write(response);
} else if (/^POST \/upload /i.test(raw)) {
const response: string = [
'HTTP/1.1 200 OK',
'Content-Type: text/html',
'Status: 200',
'',
'',
'<html><body>UPLOADED!</body></html>',
'',
'',
].join('\r\n');
socket.write(response);
}
});
});
server.listen(port, () => {
console.log(`[server] opened server: ${JSON.stringify(server.address())}`);
});