To create an HTTP server and read the request body as binary in JavaScript, you can use the built-in http
module in Node.js. Here's an example code:
index.tsx401 chars18 lines
This code creates an HTTP server using the createServer
method of the http
module. When a request arrives, the server registers a listener for the 'data'
event, which is emitted whenever a chunk of request body data arrives. The chunks are stored in an array, and when all the data has been received (the 'end'
event is emitted), the chunks are concatenated into a single Buffer
object. You can then process the binary data as needed. Finally, the server sends an HTTP response with the 'OK'
message.
To test the server, you can use a client tool such as curl
:
index.tsx75 chars2 lines
This example sends a POST
request with the binary data from the specified file to the URL of the server. You should see a message logged to the console indicating the number of bytes received.
gistlibby LogSnag