如何在 Spring Boot 中使用 create-react-app? [英] How can I use create-react-app with Spring Boot?
问题描述
我正在尝试将 ReactJS 应用程序与使用 Spring Boot 构建的 API 一起提供服务.我已经在/public/上运行脚本 create-react-app
- 如下面的结构所示 - 但是,当我尝试从
我建议你创建两个项目:
- 一个用于您的 Spring Boot 后端;
- 使用
create-react-app
创建的 React 前端.
在开发模式下,您必须使用 node: 'npm start' 运行 React 开发服务器.您应该为您的 Spring Boot 应用程序定义一个代理:"proxy": "http://localhost:8080",
以添加到您的 package.json
.此机制的文档是 此处.您可以像往常一样使用 Gradle 执行后端:./gradlew bootRun
.
在生产部署中,您可以使用 npm run build
生成 React 应用程序的构建版本.create-react-app
生成的静态文件可以放在 Spring Boot 应用程序的静态目录中,例如 src/main/resources/static/
(关于静态内容的文档 使用 Spring Boot)
我希望这会有所帮助!
I'm trying to serve a ReactJS app together with an API built with Spring Boot. I've run the script create-react-app
on /public/ - as shown on the structure below - however, all relative paths from ReactJS seems to be broken when I try to access it from http://localhost:8080/public/index.html
What else do I need to do in order to correctly load the resources from ReactJS?
I suggest you create two projects:
- one for your Spring Boot backend;
- one for your React frontend, created with
create-react-app
.
In dev mode, you have to run the React development server with node: 'npm start'.
You should define a proxy to your Spring Boot app: "proxy": "http://localhost:8080",
to add to your package.json
. Documentation of this mechanism is here.
And you can execute your backend as usual with Gradle: ./gradlew bootRun
.
In production deployment, you can generate a build of your React app with npm run build
. The static files produced by create-react-app
can be placed in a static directory in your Spring Boot application, for instance in src/main/resources/static/
(docs about static content with Spring Boot)
I hope this helps!
这篇关于如何在 Spring Boot 中使用 create-react-app?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!