Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
How to send response using twice or more callback in one request

I'm using express and the request POST look like that

router.post('/', function(req, res, next){
 var data = req.body;
 getRandom(data, function(value){
     res.json({value: value});
 });
});

POST is sent through ajax and then update textarea with new data.

 $.ajax({
           type: "POST",
           url: "/",
           data: JSON.stringify(datareq),
           dataType: 'json',
           contentType: 'application/json',
           success: function(x){
               $.each(x, function(index, value) {
                  $('.textarea').append(value + '\n');
               });
           },
           error: function(x) {
                console.log(x + 'error');
          }
       });

How to send this using one POST and a few response. User received one data in textarea when cb finished and then another data and so one till the end.

<textarea>
 data 1 - 1sec
 data 2 - 2sec leater
 data 3 - 3 second later
 ...
</textarea>

I add Time (1sec ...) only to show that callback has a lot to do to send another response.

Of course this not working because res.send() close connection and I received error

So how to achieve my idea, to sending simultaneously after post request. I want to give user data very fast, then another one when is ready not waiting for all and then send response.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can't


Reason:

Http closes connection after sending response. You can not keep it open and sending multiple responses to the client. HTTP doesn't support it.

Solution 1:

Simply put a timer at client side and request periodically.

Solution 2 (Recommended):

Use socket, and pass data through it. socket.io is the socket library for nodejs applications. It is very easy to use. Set up a connection, keep sending data from server and receive it on client side.


Just to add on the answer. This answer explains why res.send closes the connection.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda