Streaming HTTP Request Directly to Response in Node.js
This is a Node.js starting script to stream HTTP request directly into response:
It behaves almost like an echo, you get back whatever you sent. For example, use HTTPie to make a request to the above server:
We can also add the
Content-Type response header to echo back what the entire media type is after assembling all chunks.
The response should have the
Content-Type field as below:
Notice that instead of usual
Content-Length in the response header, we’ve got
Transfer-Encoding: chunked. The default transfer encoding for Node.js HTTP is
Sending a ‘Content-length’ header will disable the default chunked encoding.[^1]
About transfer encoding:
Chunked transfer encoding is a data transfer mechanism in version 1.1 of the Hypertext Transfer Protocol (HTTP) in which data is sent in a series of “chunks”. It uses the Transfer-Encoding HTTP header in place of the Content-Length header, which the earlier version of the protocol would otherwise require. Because the Content-Length header is not used, the sender does not need to know the length of the content before it starts transmitting a response to the receiver. Senders can begin transmitting dynamically-generated content before knowing the total size of that content. … The size of each chunk is sent right before the chunk itself so that the receiver can tell when it has finished receiving data for that chunk. The data transfer is terminated by a final chunk of length zero.[^2]
With the above starting script, now you can attach some transform streams to manipulate the request and stream back in chunked response.
[^1]: HTTP, Node.js API Docs
[^2]: Chunked transfer encoding, Wikipedia