How do I set headers without returning response yet?

I currently use msg.res.set('x', 'x'); which works fine except for the deprecation warnings "Deprecated call to msg.res.set". I could not find another way to do this without instantly returning response. What is the proper alternative?

I separely set the msg.statusCode and several headers on different places in the flow, all returning to the same output function that deals with those.

  • msg.res.set() works but results in a deprecation warning
  • msg.res.header() cannot be used: "TypeError: msg.res.header is not a function"
  • msg.res.headers['x'] = 'x' or something like that seems not possible because that object is probably inside _res which is [internal]

Also doesn't mention the deprecation of .set at all, so I don't understand why this would be deprecated in NodeRED.

What are you trying to do?

statusCode is set after a request to tell you the status of your request - why on earth are you trying to set it?

As for headers, read the side bar help info - I think it tells you how to set them there.

StatusCode is not really relevant here yet. Basically I have a bunch of GET endpoints that do stuff while in the meanwhile adding (if needed) some response headers (sometimes many steps before it's ready to respond), that's exactly the purpose.

Then in the end they all reach the same "output" subflow which contains a few more msg.res.set() calls to add headers, respects the statusCode set by a previous fuction and basically ends the request with http response node. All works exactly a

Is expected except the deprecation warning is annoying.

In a nutshell: I need to simply set a simple response header before the http response node is called, kind of like middleware but without implementing the full middleware.

What has been deprecated is a flow treating msg.res as a HttpResponse object and assuming it can use the functions provided by the express API. You should not be calling any of the functions on that object.

The sidebar help for the HTTP nodes describes how to set headers - create a property called msg.headers and add them to that. The HTTP Response node will use that property to set the headers of the response.

@knolleary exactly what I was looking for. Thanks. I just expected that it the express way would make more sense.

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