Created
December 13, 2019 14:13
-
-
Save iamssen/ad17fd5ed1c2787e5337fc9200dec83b to your computer and use it in GitHub Desktop.
Understanding HTTP using Node.js "net" 4. Multi-Part Form Data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment