Best practices for Node-RED proxy endpoints?

I have a flow that implements a proxy for a secure API. It has a HTTP In, which takes a bunch of parameters. The flow modifies the inputs, adds an Auth Token and calls some other HTTP request node to perform the task. The flow receives the output and then replies to the original request with the answer.

[HTTP-IN]-[function and Change nodes]-[httprequest]-[function and Change nodes]-[http-response-out]

I have a Change node which moves the orginial msg.res and msg.req objects to backups, clears the msg.headers and builds new headers for the HTTP request. Then, after the HTTP request (which works fine), the flow stores the msg.payload, does a msg = {}, restores the msg.res and msg.req, builds a new msg.payload with the results, and then wires to the HTTP response/out node. I didn't mess with msg._msgid

This worked for many months with a spurious

 msg : error
"Error: Callback called multiple times"

Recently, I restructured some of the flows and now it is completely failing with a

msg : error
"TypeError: Illegal invocation"

and the downstream mobile app never gets the response.

I thought it was due to Node-RED v1.x / v2.x http node migration from requests to got. I fall back to Node-RED 1.3.6 but the error persists.

A Catch node report two errors:

{"message":"TypeError: Illegal invocation","source":{"id":"bab29f02.3f0138","type":"http response","count":1},"stack":"TypeError: Illegal invocation\n    at writevGeneric (internal/stream_base_commons.js:134:26)\n    at Socket._writeGeneric (net.js:783:11)\n    at Socket._writev (net.js:792:8)\n    at doWrite (internal/streams/writable.js:375:12)\n    at clearBuffer (internal/streams/writable.js:521:5)\n    at Socket.Writable.uncork (internal/streams/writable.js:317:7)\n    at ServerResponse.end (_http_outgoing.js:829:17)\n    at ServerResponse.end (/opt/app-root/data/node_modules/compression/index.js:107:21)\n    at ServerResponse.send (/opt/app-root/data/node_modules/express/lib/response.js:221:10)\n    at ServerResponse.jsonp (/opt/app-root/data/node_modules/express/lib/response.js:335:15)\n    at HTTPOut._inputCallback (/opt/app-root/data/node_modules/@node-red/nodes/core/network/21-httpin.js:328:53)\n    at /opt/app-root/data/node_modules/@node-red/runtime/lib/nodes/Node.js:203:26\n    at Object.trigger (/opt/app-root/data/node_modules/@node-red/runtime/lib/hooks.js:113:9)\n    at HTTPOut.Node._emitInput (/opt/..."}

and

{"message":"Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client",
"source":{"id":"66b84803d9695bbd",
"type":"http response","count":1},
"stack":"Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client\n    at ServerResponse.setHeader (_http_outgoing.js:558:11)\n    at ServerResponse.header (/opt/app-root/data/node_modules/express/lib/response.js:771:10)\n    at ServerResponse.header (/opt/app-root/data/node_modules/express/lib/response.js:774:12)\n    at HTTPOut._inputCallback (/opt/app-root/data/node_modules/@node-red/nodes/core/network/21-httpin.js:307:34)\n    at /opt/app-root/data/node_modules/@node-red/runtime/lib/nodes/Node.js:203:26\n    at Object.trigger (/opt/app-root/data/node_modules/@node-red/runtime/lib/hooks.js:113:9)\n    at HTTPOut.Node._emitInput (/opt/app-root/data/node_modules/@node-red/runtime/lib/nodes/Node.js:195:11)\n    at HTTPOut.Node.emit (/opt/app-root/data/node_modules/@node-red/runtime/lib/nodes/Node.js:179:25)\n    at HTTPOut.Node.receive (/opt/app-root/data/node_modules/@node-red/runtime/lib/nodes/Node.js:476:10)\n    at Immediate._onImmediate (/opt/app-root/data/node_modules/@no..."}

I don't know what the issue is but ...

That might be an issue - those objects are absolutely enormous and have circular references as well. You really do not want to be taking copies of them. If you need any specific data from them, just take that, otherwise you will want to get rid of them.

The msg.res and msg.req are somewhat required to response to the original http-in.

After much head banging, I fixed this by deleting the msg._msgid

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.