Home > database >  How to handle two long request simultanously in expressJS
How to handle two long request simultanously in expressJS

Time:01-13

i have an API with express one route make a few time to get all data required (search through long JSON object)

router.get(
  "/:server/:maxCraftPrice/:minBenef/:from/:to",
  checkJwt,
  async (req, res) => {
    const getAllAstuces = new Promise(async (resolve, reject) => {
      const { EQUIPMENTS_DIR, RESOURCES_DIR } = paths[req.params.server];
      const astuces = [];
      const { from, to, maxCraftPrice, minBenef } = req.params;
      const filteredEquipments = getItemByLevel(from, to);
      for (const equipment in filteredEquipments) {
        // parsing and push to astuces array
      }
      resolve(astuces);
    });

    const resource = await getAllAstuces;
    return res.json(resource);
  }
);

Now in my website when someone go to the page associated with this route, while the data is loading EVERY other request is just locked like in a queue

I tried to add Promise to handle this but no change

Is there a way to handle requests simultanously or maybe should i refactor that route to make it faster ?

CodePudding user response:

If your request takes a long time to process, it will block all other requests until it is done. If you can make the request take less processing time, that's a good place to start, but you're probably going to need to take further steps to make multiple requests faster.

There are various methods for getting around this situation. This article describes a few approaches.

  •  Tags:  
  • Related