Set Response header for HTTP Request

I am trying to send response header in all HTTP request:
I tried two way:
First by adding below code in settings.js

    httpNodeMiddleware: function(req,res,next) {
	res.header("X-Frame-Options", "SAMEORIGIN");

Above code is not working,
Second: I wrote below code in red.js then it is working as expected.

 app.use(function(req, res, next) {
   res.header("X-Frame-Options", "SAMEORIGIN");

Can anyone tell me why first code is not woring?

1 Like

You need to read the comments in the settings.js file:

    // The following property can be used to add a custom middleware function
    // in front of all http in nodes. This allows custom authentication to be
    // applied to all http in nodes, or any other sort of common request processing.

It states that the middleware function applies to http in nodes. Not to request nodes. Your second example works because you have manually forced Node-RED to always send that header.

For request nodes, you can set headers using the input msg. Set msg.headers as it says in the help for that node.

1 Like

I want to implement as middleware.

@TotallyInformation I don't think your interpretation is right. The question is about setting a "response header" not a request header.

@Devbrat at a glance, I'd expect your httpNodeMiddleware function to do what you want. Will need to debug it proper to figure out why it isn't working.

@knolleary That would be great help. Thanks in advance.

Oops, mea culpa.