我应该如何配置create-react-app从子目录提供服务? [英] How should I configure create-react-app to serve app from subdirectory?
问题描述
我在服务器上呈现了经典的Web应用程序。我想在React中将管理面板创建为单页应用程序。我想从 https://smyapp.example.com/admin/ 服务器管理面板。我尝试使用 create-react-app
,但是它假定我从根URL提供SPA。如何配置 create-react-app
来从 admin
子目录提供应用程序?在文档中,我找到了首页
属性,但如果我正确理解,它需要完整的URL。我无法提供完整的网址,因为我的应用程序部署在少数环境中。
I have classic web application rendered on server. I want to create admin panel as single page application in React. I want to server admin panel from https://smyapp.example.com/admin/. I try to use create-react-app
but it assumes that i serve SPA from root URL. How should I configure create-react-app
to serve app from "admin"
subdirectory? In documentation I found "homepage"
property but if I properly understand it requires complete url. I can't give complete url because my app is deployed in few environments.
推荐答案
对于create-react-app v2并做出反应-router v4,我使用以下组合在 / app下为生产(登台,uat等)应用程序提供服务:
For create-react-app v2 and react-router v4, I used the following combo to serve a production (staging, uat, etc) app under "/app":
package.json:
package.json:
"homepage": "/app"
然后在应用程序入口点:
Then in the app entry point:
<BrowserRouter basename={process.env.PUBLIC_URL}>
{/* other components */}
</BrowserRouter>
一切在本地开发环境和部署环境中都有效。 HTH!
And everything "just works" across both local-dev and deployed environments. HTH!
这篇关于我应该如何配置create-react-app从子目录提供服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!