简体   繁体   English

Spring Boot和React:在Spring应用程序的index.html中呈现app.js

[英]Spring Boot and React: Rendering app.js in index.html of Spring application

In this tutorial on using String Data REST with React it says to create an HTML file called index.html in the Spring Boot project and have a div where I can render the React component. 本教程中,将String Data REST与React结合使用时,它说在Spring Boot项目中创建一个名为index.html的HTML文件,并具有一个div ,可在其中渲染React组件。

The problem I have is my React app is a completely different project. 我的问题是我的React应用程序是一个完全不同的项目。 How do I do this or what else can I do to fix this? 我该怎么做,或者我还能做些什么来解决这个问题? Thank you very much for the help! 非常感谢你的帮助!

You could do two things: 您可以做两件事:

  1. You could run them both in separate projects as you have it now and run them on different ports locally, however that means you need to enable cross-origin requests on your server or configure a proxy in the react project. 您可以像现在一样在单独的项目中运行它们,然后在本地的不同端口上运行它们,但这意味着您需要在服务器上启用跨域请求或在react项目中配置代理。 (If you used create-react-app you can just add "proxy": "http://localhost:8080" in your package.json file where http://localhost:8080 is the url to your backend.). (如果使用create-react-app,则可以在package.json文件中添加"proxy": "http://localhost:8080" ,其中http:// localhost:8080是后端的URL。)

  2. You could move all the files into one project and have your React project run on the embedded Spring server. 您可以将所有文件移动到一个项目中,并使您的React项目在嵌入式Spring服务器上运行。 However this requires some re-configuration of your webpack.config.js and some restructuring of files. 但是,这需要对webpack.config.js一些重新配置, webpack.config.js文件进行一些重组。

I suggest the first option which is easier and also probably how you would like to run your application in the future, with one container for the client-side code and one container for your backend code. 我建议使用第一个选项,它更容易,也可能是将来您希望如何运行应用程序,其中一个容器用于客户端代码,而一个容器用于后端代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM