【问题标题】:Simplest way to put everything under http://localhost:3000/app instead of http://localhost:3000/ in React在 React 中将所有内容放在 http://localhost:3000/app 下而不是 http://localhost:3000/ 下的最简单方法
【发布时间】:2019-10-20 12:04:21
【问题描述】:

如何更改 React 应用的根路径,具体来说:所有 html、css 和 js 都将在 http://localhost:3000/app 下,而不仅仅是 http://localhost:3000?

有没有一种简单的方法来修改现有应用程序以增加一个级别?

(这在 React 前面有反向代理时很有用,例如 nginx 或 apache)

请注意,编译后的项目也应该到处都有相同的 /app 前缀

非常感谢

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    找到编译代码的解决方案!

    添加到package.json

      "homepage": "/app/",
    

    添加到index.html

    <head>
      <base href="%PUBLIC_URL%/">
    </head>
    

    现在使用npm run build 构建应用程序

    将文件复制到网络服务器(例如 Nginx)cp -r build /usr/share/nginx/html/app

    访问:

    http://localhost/app
    

    所以现在所有路径都应该有前缀app/

    【讨论】:

      猜你喜欢
      • 2010-12-01
      • 1970-01-01
      • 2019-07-07
      • 2010-10-07
      • 1970-01-01
      • 2016-03-18
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      相关资源
      最近更新 更多