问题
I have an app created by create-react-app. After building, it generates a html file with some JS and CSS files. The html file looks like
<!doctype html>...<script src="/static/js/main.89f33fbb.chunk.js"></script></body></html>
I try to host those static files by an Express.js server.
const app = express()
.use(bodyParser.json())
.use(express.static(path.join(__dirname, '../dist')))
app.listen(5000, '0.0.0.0');
When I try to open http://127.0.0.1:5000 or http://localhost:5000 in browser, it has no issue to load the whole page.
However, when I try to open http://0.0.0.0:5000, it first gets the html file successfully. However, in the following requests, it tries to get JS and CSS files through https
that do not exist.
I know the <script src="/static/js/main.89f33fbb.chunk.js">
will try to get the file through https
first, but if not exist, I expect it to get through http
.
I tried Chrome, Firefox, and Safari in private mode, and the results are same.
Is there any place I did wrong?
回答1:
You can listen on any address using 0.0.0.0, but you should not use it to send a request, because 0.0.0.0 is a non-routable meta-address used to designate an invalid, unknown or non-applicable target.
In the context of servers, 0.0.0.0 means "all IPv4 addresses on the local machine". If a host has two ip addresses, 192.168.1.1 and 10.1.2.1, and a server running on the host listens on 0.0.0.0, it will be reachable at both of those IPs, but if you want to reach the server, you should use one of those Ip.
Take a look at:
- What's the difference between 127.0.0.1 and 0.0.0.0?
- What does Chrome/server do when I use 0.0.0.0 instead of localhost in browser?
- What's the difference between IP address 0.0.0.0 and 127.0.0.1?
来源:https://stackoverflow.com/questions/65151137/why-does-the-browser-try-to-get-files-through-https-when-the-host-is-0-0-0-0