是否可以将 React 应用程序用户从 127.0.0.1:3000 重定向到 localhost:3000?

Is it possible to redirect React app users from 127.0.0.1:3000 to localhost:3000?

我正在与其他定期导航到 http://127.0.0.1:3000/ 到 view/use 应用程序的开发人员一起开发 React 应用程序。当通过 http://127.0.0.1:3000/ 访问应用程序时,它是部分功能的,这会造成混淆。是否可以将用户从 http://127.0.0.1:3000/ 重定向到 http://localhost:3000/react-router-dom 中的 <Route /> 组件似乎只处理(顾名思义)应用程序路由(而不是 domain/port)。

这应该有效。将它放在你的应用程序组件中,它会监视网址并检查它是否包含 127.0.0.1:3000。如果是这样,它将重定向到您的 localhost.

if (window.location.href.includes("127.0.0.1:3000"){
  window.location.replace("http://localhost:3000/")
}

更好的是,如果您想维护用户当前所在的页面,只需将 127.0.0.1:3000 替换为 localhost:3000:

const currentDomain = window.location.href
 
if (currentDomain.includes("127.0.0.1:3000"){
  newDomain = currentDomain.replace("127.0.0.1:3000", "localhost:3000")
  window.location.replace(newDomain)
}