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

102
Vistas
Error with zmq and express

I'm trying to create an API that sends an information obtained by a socket.
To get the information from my socket, I'm using zmq.
The idea is when got the information from the socket, send this info as a response in my API. The following code pretends do that.

[...]
enclaveSocket.on('message', function (message) {

    res_json.info = message.toString('base64');

    res.json(res_json);
});
enclaveSocket.send('chesf:public');
[...]

In the first request to my express API, the return is the expected:

{
  "organization": "my_org",
  "info": "AjCWyQeBNeuMnA3nAUrbprxctrdOgZmjDR5Rmuw6LzzMpALspfNapLMoRGPx9YEGFrqrE5vdHKPb/z0S23i8v"
}

But in the second request my API crashes with the following error:

events.js:160
  throw er; // Unhandled 'error' event
  ^

Error: Can't set headers after they are sent.
at ServerResponse.setHeader (_http_outgoing.js:367:11)
at ServerResponse.header (/home/matteus/keyvault-node/node_modules/express/lib/response.js:725:10)
at ServerResponse.send (/home/matteus/keyvault-node/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/home/matteus/keyvault-node/node_modules/express/lib/response.js:256:15)
at exports.Socket.<anonymous> (/home/matteus/keyvault-node/routes/keys.js:58:21)
at emitOne (events.js:101:20)
at exports.Socket.emit (events.js:188:7)
at exports.Socket.Socket._flushRead (/home/matteus/keyvault-node/node_modules/zmq/lib/index.js:636:10)
at exports.Socket.Socket._flushReads (/home/matteus/keyvault-node/node_modules/zmq/lib/index.js:676:23)
at Object._zmq.onReadReady (/home/matteus/keyvault-node/node_modules/zmq/lib/index.js:297:10)

That's it.

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

0

I solved my problem using enclaveSocket.once() instead of enclaveSocket.on().
The once() method removes event listeners when the event is handled by the same function.

More information about here: http://nodeguide.com/beginner.html#using-eventemitters

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