2
2
210mev2021-03-01 11:34:45
Node.js
210mev, 2021-03-01 11:34:45

How to handle many requests?

Good afternoon,
I'm learning NodeJS, I'm already a little confused.

There is an API: nginx -> pm2 -> nodejs
There is a service that sends JSON to this API, from 10 to 100 objects per second in one request.
How to make it so that not to miss a single request (parcel) despite the fact that you need to send this data to another service?
Here is the code

app.post('/apiendpoint', function (request, reply) {

  let arrayOfRequestsToSend = [];

  for (let index = 0; index < request.body.user; index++) {

    if (request.body.user.user_id) {
      let userId = request.body.user.user_id;
      let userName = request.body.user.user_name;
      // что будет если здесь асинхроно записывать в БД userId и userName?
      let requestToSend = axios.get(`http://someservice.com/someendpoint?user_id=${userId}&user_name=${userName}`);
      arrayOfRequestsToSend.push(requestToSend);
    }
  }
  // переслать полученые данные на другой сервис
  if (arrayOfRequestsToSend.length != 0) {
    axios.all(arrayOfRequestsToSend)
      .then(responseArr => {
        console.log(responseArr);
      })
      .catch(errors => {
        console.log(errors);
      });
  }
  reply.send(request.query);
})

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question