简体   繁体   中英

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.

The problem I have is my React app is a completely different project. 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. (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.).

  2. You could move all the files into one project and have your React project run on the embedded Spring server. However this requires some re-configuration of your webpack.config.js and some restructuring of files.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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