Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stream response in express?

I've been trying to get a express app to send the response as stream.

var Readable = require('stream').Readable; var rs = Readable();   app.get('/report', function(req,res) {          res.statusCode = 200;     res.setHeader('Content-type', 'application/csv');     res.setHeader('Access-Control-Allow-Origin', '*');      // Header to force download     res.setHeader('Content-disposition', 'attachment; filename=Report.csv');           rs.pipe(res);      rs.push("USERID,NAME,FBID,ACCOUNT,SUBSCRIPTION,PRICE,STATE,TIMEPERIOD\n");      for (var i = 0; i < 10; i++) {         rs.push("23,John Doe,1234,500,SUBSCRIPITON,100,ACTIVE,30\n");     }      rs.push(null); });       

It does print in the console when I replace "rs.pipe(res)" by "rs.pipe(process.stdout)". But how to make it work in an express app?

Error: not implemented     at Readable._read (_stream_readable.js:465:22)     at Readable.read (_stream_readable.js:341:10)     at Readable.on (_stream_readable.js:720:14)     at Readable.pipe (_stream_readable.js:575:10)     at line "rs.pipe(res);" 
like image 303
Amit Adhikari Avatar asked Aug 05 '16 11:08

Amit Adhikari


People also ask

How do I send a response in Express?

How to send a response back to the client using Express. In the Hello World example we used the Response. send() method to send a simple string as a response, and to close the connection: (req, res) => res.

How do I get response time in Express?

We can get the response time in the response header of a request with the response-time package. It has a responseTime function which returns a middleware that we can use with the use method of express or express. Router() .

How do I make a writable stream?

To write data to a writable stream you need to call write() on the stream instance. Like in the following example: var fs = require('fs'); var readableStream = fs. createReadStream('file1.


2 Answers

You don't need a readable stream instance, just use res.write():

res.write("USERID,NAME,FBID,ACCOUNT,SUBSCRIPTION,PRICE,STATE,TIMEPERIOD\n");  for (var i = 0; i < 10; i++) {     res.write("23,John Doe,1234,500,SUBSCRIPITON,100,ACTIVE,30\n"); }  res.end(); 

This works because in Express, res is based on Node's own http.serverResponse, so it inherits all its methods (like write).

like image 180
robertklep Avatar answered Oct 05 '22 13:10

robertklep


I was able to get this to work.

  • Put this code in your Express router.
  • Open a web browser
  • Go to http://yourdomain/yourrouterpath/stream

...

router.get('/stream', function (req, res, next) {   //when using text/plain it did not stream   //without charset=utf-8, it only worked in Chrome, not Firefox   res.setHeader('Content-Type', 'text/html; charset=utf-8');   res.setHeader('Transfer-Encoding', 'chunked');    res.write("Thinking...");   sendAndSleep(res, 1); });   var sendAndSleep = function (response, counter) {   if (counter > 10) {     response.end();   } else {     response.write(" ;i=" + counter);     counter++;     setTimeout(function () {       sendAndSleep(response, counter);     }, 1000)   }; }; 
like image 23
Sagan Avatar answered Oct 05 '22 13:10

Sagan