Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid Host Header when ngrok tries to connect to React dev server

I'm trying to test my React application on a mobile device. I'm using ngrok to make my local server available to other devices and have gotten this working with a variety of other applications. However, when I try to connect ngrok to the React dev server, I get the error:

Invalid Host Header 

I believe that React blocks all requests from another source by default. Any thoughts?

like image 382
Patrick Connors Avatar asked Oct 08 '22 04:10

Patrick Connors


People also ask

How do you use Ngrok With react?

Host a React App With Ngrok To get started, install nodejs and npm by executing the commands below on the Terminal. , as shown below. By typing the URL on the browser, you can see the default React app. It is simply a rotating logo and a welcome message.

What is host in HTTP header?

The Host request header specifies the host and port number of the server to which the request is being sent. If no port is included, the default port for the service requested is implied (e.g., 443 for an HTTPS URL, and 80 for an HTTP URL). A Host header field must be sent in all HTTP/1.1 request messages.


1 Answers

I'm encountering a similar issue and found two solutions that work as far as viewing the application directly in a browser

ngrok http 8080 --host-header="localhost:8080"
ngrok http --host-header=rewrite 8080

obviously, replace 8080 with whatever port you're running on

this solution still raises an error when I use this in an embedded page, that pulls the bundle.js from the react app. I think since it rewrites the header to localhost when this is embedded, it's looking to localhost, which the app is no longer running on

like image 128
textual Avatar answered Oct 15 '22 13:10

textual