1

I have a Rest API in node.js that takes much time to respond because it sends a request to suppliers vendor and once the response is fully prepared then it returns the result what I want is as the result is being prepared it should be able to display it on the front react side. Thanks in advance for any help and your time

here is my controller

module.exports.search = async (req, res) => {
  try {
    let params = req.query;
    params = _.extend(params, req.body);
    const result = await modules.Hotel.Search.search(req.supplierAuth, params);
    res.json(result);
  } catch (e) {
    global.cli.log('controller:hotels:search:  ', e);
    res.status(500).json({ message: e.message });
  }
};

here is my front side service

export const getHotels = (filters, options = {}) => {
  const queryString = new URLSearchParams(options).toString();
  return post(`/api/hotels/search?${queryString}`, filters);
};
Yash Karanke
  • 764
  • 1
  • 15
  • 29

1 Answers1

0

The best solution is to use streams and pipe() the results as they come into express's res object, similar to this guy's approach.

You'll have to modify the modules.Hotel.Search.search(....) and make that use streams.

Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72