Categories
JavaScript Answers

How to stream response in Express and JavaScript?

Spread the love

Sometimes, we want to stream response in Express and JavaScript.

In this article, we’ll look at how to stream response in Express and JavaScript.

How to stream response in Express and JavaScript?

To stream response in Express and JavaScript, we use res.write.

For instance, we write

app.get("/report", (req, res) => {
  res.write("USERID,NAME,FBID,ACCOUNT,SUBSCRIPTION,PRICE,STATE,TIMEPERIOD\n");

  for (let i = 0; i < 10; i++) {
    res.write("23,John Doe,1234,500,SUBSCRIPITON,100,ACTIVE,30\n");
  }

  res.end();
});

to call res.write to put each line in the response.

Then we call res.end to stop sending the lines.

Conclusion

To stream response in Express and JavaScript, we use res.write.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *