ReactJS APP in Heroku "Invalid Host header" HOST configuration?
Solution 1
If for any reason you were trying to deploy the client without the server, make sure to remove the:
"proxy": "http://localhost:5000"
from the package.json of the client..
Edit July 2019:
Create React App 2.0 has changed how we define Proxies. To determine which version you are using, check your client side package.json: "react-scripts" greater than "2.x.x"
Now in the client/ directory install this package:
npm install http-proxy-middleware --save
Create setupProxy.js file in client/src/ directory. There is no need to import this file anywhere, CRA looks for a file by this name and loads it.
There are different ways to add proxies:
Option 1:
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
app.use(
createProxyMiddleware(["/api", , "/otherApi"], { target: "http://localhost:5000" })
);
};
Option 2
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
app.use(createProxyMiddleware('/api/**', { target: 'http://localhost:5000' }));
app.use(createProxyMiddleware('/otherApi/**', { target: 'http://localhost:5000' }));
};
Answering to comments
This proxy is just used in development environment. In production/Heroku everything runs under the same server, so there is not need for Proxy there.
create-react-app server just runs in Dev environment, so when the application is run in PROD mode, it is just used to generate the production JS bundle that will be served by the Node/Express server.
Check this other answer for questions on how to make it work in production.
Solution 2
Invalid Host Header
has been put in as a solution to DNS Rebinding.
To solve this, you have to create a file named .env.development in the create-react-app
root folder. Inside this file, set
HOST=name.herokuapp.com
From the Documentation: https://create-react-app.dev/docs/proxying-api-requests-in-development/#invalid-host-header-errors-after-configuring-proxy
Related videos on Youtube
fourth
Updated on July 09, 2022Comments
-
fourth almost 2 years
I am trying to put my React app on the Heroku. The whole project include one API (express) and one client (ReactJS). I have put my API on heroku. But when I put my client on Heroku, it shows build succeeded. But when I open it, it shows
Invalid Host header
.I google this problem and many people tell me to configure the HOST. But they are using webpack. I build this with
create-react-app
and I run it bynpm start
. I want to know how to solve this problem in most easy way. Thanks. -
Jake almost 5 yearsInside the root of the client or the server?
-
Shane almost 5 yearsBut how do react send request to backend if you remove the proxy? You explicitly specify the path?
-
Roberto Rodriguez almost 5 years@Kittichote Kamalapirat I updated my answer to answer your question. I hope it helps
-
Shane almost 5 yearsThank you very much, @Roberto. What about if there is the server? For example, Node, React app? You're not supposed to use proxy for that right? Do you have to
run build
in client to create the static files for production? -
Roberto Rodriguez over 4 yearsProxies are used just in development mode, where React App is running in one port and Express server in other. In production mode normally everything will run under the same port.
-
s2t2 over 4 yearsWhen I set the proxy URL to my heroku API server, I see "ECONNREFUSED" (No connection could be made because the target machine actively refused it. This usually results from trying to connect to a service that is inactive on the foreign host.)
-
dam over 4 yearsWhen I follow the instructions above it works in my local dev environment but when deployed to heroku I see a 504 error in browser and in the heroku logs: [HPM] Error occurred while trying to proxy request /api/test-get?id=1 from basic-dm.herokuapp.com to localhost:5000 (ECONNREFUSED) (nodejs.org/api/errors.html#errors_common_system_errors). I have verified that the API service is correctly responding by calling it directly with the heroku URL so it is up and running. So the proxy through react isn't working
-
Roberto Rodriguez over 4 yearss2t2 and dam I updated my answer for you guys, I hope it helps.
-
DataGuru almost 4 years@RobertoRodriguez - you are my saviour. I have been fighting this for the past 3 days; Thank you The edit I would suggest is const { createProxyMiddleware } = require('http-proxy-middleware'); module.exports = function(app) { app.use(createProxyMiddleware('/flask/**', { target: 'localhost:5000' })); };
-
rickvian almost 3 yearsthis cannot solve my issue, my local teriman log: Could not find an open port at name.herokuapp.com.