Wednesday, 7 October 2020

Avoid re-fetching data while streaming in Expressjs

I've just started playing with streaming data in Expressjs.

Not entirely sure, but I think the request will start to execute the handler again. For example, here is my handler:

import getDataAsync from "./somewhere";

function handler(req, res) {

    console.log('requesting', req.path);

    getDataAsync()
        .then(data => {
            let stream = renderContent(data);
            stream.pipe(res);
        })
        .catch(err => {
            res.end();
        })
}

What I found was, it continue to print out console.log('requesting', req.path) (which I think will re-execute getDataAsync).

My question is:

Is it true it will re-execute getDataAsync? If it does, what's your approach?

Thank heaps!



from Avoid re-fetching data while streaming in Expressjs

No comments:

Post a Comment