Reputation: 729
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 by npm start
. I want to know how to solve this problem in most easy way. Thanks.
Upvotes: 17
Views: 32340
Reputation: 3
In my case, the problem was caused by environment variables used in development, not being read in production because the .env file, containing the environment variables, was included in my .gitignore
To solve it, I added the environment variables to the Heroku host. From Heroku app: settings -> Congif Vars -> Reveal Config Vars
Then add the same environment variables you used in development, here. Once done, the error was gone!
environment variable being used in app
const accessToken = jwt.sign(
{ "userName": foundUser.userName },
process.env.REACT_APP_ACCESS_TOKEN_SECRET,
{ expiresIn: '2h' },
);
const refreshToken = jwt.sign(
{ "userName": foundUser.userName },
process.env.REACT_APP_REFRESH_TOKEN_SECRET,
{ expiresIn: '1d' },
);
same environment variable in Heroku, Config Vars
Upvotes: 0
Reputation: 535
In my case, this was happening when I was running the React app in dev mode with webpack. I solved it by setting the value for allowedHosts
in the webpack.config.js
file. You can check out more about the allowedHosts property from the official webpack documentation.
For my case, I added the following code into module.exports
of webpack.config.json
:
module.exports = {
// Some stuff that already exists before
//.
//.
//.
devServer: {
allowedHosts: 'all'
},
}
NOTE: It is not recommended that you use all
for the allowedHosts
property due to security issues. Be sure to specify specific hosts that the app must run on.
Upvotes: 0
Reputation: 3347
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.
Upvotes: 29
Reputation: 19204
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
Upvotes: 9