To return a binary response from an HTTP server in TypeScript, you need to set the response headers to indicate that the response contains binary data, and then send the binary data as the response body. Here's an example:
index.ts547 chars20 lines
In this example, we create an HTTP server using the http
module, and in the request handler, we set the response headers to indicate that the response contains binary data. We then read the binary data from a file using the fs
module, and send it as the response body using the res.write()
method. Finally, we end the response using the res.end()
method.
Note that in this example, we set the response headers to indicate that the response should be downloaded as a file (Content-Disposition: attachment; filename="binary-file"
). You can modify the headers to suit your specific use case.
gistlibby LogSnag