0

I want to remove / modify my react.js's response headers.

I want to for example remove the header "X-Powered-By".

Is there any way to do this?

Edit: I want to do this in REACT.JS, not express.

Hobey823
  • 276
  • 4
  • 13
  • React is a frontend framework, it's not setting any headers. Are you talking about headers set by the `create-react-app` dev server? – UncleDave Oct 16 '19 at 16:56
  • Yes, and production. – Hobey823 Oct 16 '19 at 16:58
  • 1
    You shouldn't be using the `create-react-app` dev server for production, it's not secure or optimised. Regardless, I don't think the dev server exposes the kind of customisation you want, as it's simply not intended for that. – UncleDave Oct 16 '19 at 17:01
  • How would i do it in production? – Hobey823 Oct 16 '19 at 17:02
  • You'd use a different web server, such as express or nginx, and refer to @jperl's answer about the former. If you have some idea of where you're deploying then that may influence your decision about which web server to use. – UncleDave Oct 16 '19 at 17:04
  • So you would setup your react.js with express? Whats the point in react-router then – Hobey823 Oct 16 '19 at 17:21
  • 1
    They are completely different things. Express is a web server, it is used to (among other things) serve static files such as `index.html` and `foo.js`. `react-router` is a library used within the React single page app to simulate browser navigation without making any further file requests to your web server. You use a web server like express to send your app to the client, then the app runs and `react-router` says "Oh, you're looking for the route `/foo`? I happen to know that means you want component `Foo`, I'll load it for you." – UncleDave Oct 16 '19 at 19:38
  • Yea i know, but you're telling me you can't modify headers in react.js - So I'm asking what's the point of doing react routering if you're gonna set it up with express? – Hobey823 Oct 16 '19 at 21:09
  • 1
    The only thing that express serves is the static files, those have headers. In-app navigation is handled by `react-router` and no further requests for static files are made, and therefore there are no headers on in-app navigations. – UncleDave Oct 16 '19 at 21:29

2 Answers2

0

React.JS as such does not generate any HTTP headers. HTTP headers may be added by your web server or possibly by a REST client library such as Axios. Please share more information on your setup.

Ricardo Gonzalez
  • 1,827
  • 1
  • 14
  • 25
Tomasz Słota
  • 126
  • 1
  • 8
-1

Disabling Express's default X-Powered-By is as easy as:

const express = require('express')
const app = express()

app.disable('x-powered-by');

See

https://expressjs.com/en/api.html#app.settings.table

https://expressjs.com/en/api.html#app.disable

If you want to modify headers:

..., function(req, res) {
  ...
  res.set('Content-Type', 'text/plain');
  ...
} 

Your question already has an answer here: How can I set response header on express.js assets

jperl
  • 4,662
  • 2
  • 16
  • 29