我正在關注一個教程,該教程將以下行添加到hosts檔案中:
127.0.0.1 posts.com
并使用這個地址來啟動他的 React 應用程式,而不是localhost:3000. 但在我的 Windows 機器上,它不能在posts.com但在posts.com:3000. 為什么會發生這種情況,我該如何解決?
以下是我的hosts檔案內容:
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 posts.com
# Added by Docker Desktop
10.24.153.39 host.docker.internal
10.24.153.39 gateway.docker.internal
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section
uj5u.com熱心網友回復:
默認情況下,您的開發服務器,無論是什么(如果您發布該資訊,我可以提供更好的幫助)在埠 3000 上提供 HTTP。主機檔案只能偽造域,而不是埠。
當您訪問域時,Web 瀏覽器使用的默認 HTTP 埠是 80(如果您使用的是 HTTP,則為 443),如果服務器托管在與 80 不同的埠上,則必須在 URL 中手動定義。
如果您不需要在 dev 下的 URL 中手動定義它,則必須重新配置您的開發服務器,以便在埠 80 上提供服務(如果使用 HTTPS,則為 443)。
如何做到這一點是特定于開發工具的。例如,如果您使用的是 create-react-app,您可以將start腳本更改package.json為 this 然后重新啟動它:
"start": "set PORT=80 && react-scripts start"
但是如果它像 webpack dev server 或 vite,你需要更改 webpack/vite 配置中的埠選項。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/508415.html
標籤:反应 重定向 windows-10 主机
