I was searching the web and documentation for node.js express module and it seems there is no way to send data by parts. I have a file rendered not very fast and I want to send parts of it before everything is rendered.
So here are my questions:
response to send data by parts?response.end()?Sample simplified code:
app.get(..) {
renderFile(file, function(data) {
response.send(data);
});
response.end();
)
This piece of code sends only the first chunk of data. I checked - data is given correctly and callback is called more than one time.
Of course I can append data to the one variable and then write response.send(data); but I don't like this approach - it is not the way it should work.
The response object is a writable stream. Just write to it, and Express will even set up chunked encoding for you automatically.
response.write(chunk);
You can also pipe to it if whatever is creating this "file" presents itself as a readable stream. http://nodejs.org/api/stream.html
Express extends Connect which extends HTTP I believe. Looking at the HTTP API, it seems that what you are looking for is response.write, which takes a chunk of data to send as a response body. I then believe you can use response.end on the end signal.
The response object can be written to. In your case, the following would be a solution:
renderFile(file, function(data) {
response.send(data);
});
response.end();
If you have the opportunity to get the file as a stream this can be streamlined further:
file.getReadableStream().pipe(response);