Expressjs dealing with high traffic

So I want to know how do I wait for response to be sent to the user ?

lets say my code has some sort of external requests that takes few seconds to response so using async functions the code waits for the response of that request then process it and sends the result back to the user, but it works only for 1 user meaning that if i request like 100 requests per time I will be getting a response but with conflicts of the entered data
like the request number 62 had the response of the request number 33 and so.
how do I mange this ? are there any libraries or methods to overcome it or it’s just some sort of errors in my code ?

1 possible answer(s) on “Expressjs dealing with high traffic

  1. I assume your issue raised due to not all variables declared in lexical scope. It is not an issue with express nor Node.js – it is a nature of pure JS: you need to use var/let/const for variables – otherwise global variables will be created implicitly.

    Please try following updated code with proper variables declaration:

    const fetch = require("node-fetch");
    const config = require("../../../config/configs")
    
    async function getquestion (questionid) {
      const url = config.url
      const options = {
        headers: {
          'Authorization': config.Authorization,
          'Accept': config.Accept,
        }
      };
    
      try {
        const response = await fetch(url, options);
        const json = await response.json();
        return json;
      } catch (error) {
        console.log(error);
      }
    }
    
    module.exports = {
      getquestion
    }
    async function asyncer(id){
      const response = await getquestion(id);
      const question = response.result.content.content
      const answer = null; // answer should be defined in case it not global variable
      return [question, answer]
    }
    const qa = await asyncer(id)
    if (qa) {
     return await res.status(200).json({
          success: 1,
          question: qa[0],
          answer: qa[1]
     })
    }