Tuesday, 2 May 2017

Error with zmq and express

I'm trying to create an api that sends an information obtained by socket. To get the information from my socket, I'm using zmq. The idea is when get 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 fisrt 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. Can anyone help me?

[]'s



via Matteus Sthefano

No comments:

Post a Comment