如何在Spring Boot中使用create-react-app?

问题描述 投票:7回答:1

我正在尝试与使用Spring Boot构建的API一起提供ReactJS应用程序。 我在/ public /上运行了脚本create-react-app ,如下面结构所示 - 但是,当我尝试从http:// localhost:8080 / public访问时,来自ReactJS的所有相对路径似乎都被破坏了/index.html

为了正确加载ReactJS中的资源,我还需要做些什么?

在此输入图像描述

reactjs spring-boot
1个回答
14
投票

我建议你创建两个项目:

  • 一个用于Spring Boot后端;
  • 一个用于你的React前端,用create-react-app

在开发模式下,您必须使用节点''npm start'运行React开发服务器。 您应该为Spring Boot应用程序定义一个代理: "proxy": "http://localhost:8080",添加到您的package.json 。 这个机制的文档在这里 。 您可以像往常一样使用Gradle执行后端: ./gradlew bootRun

在生产部署中,您可以使用npm run build生成React应用npm run buildcreate-react-app生成的静态文件可以放在Spring Boot应用程序的静态目录中,例如src/main/resources/static/有关 Spring Boot的静态内容的文档

我希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.