Reputation: 615
I am following this blog post to create a spring boot with reactjs project.
In order for my project to work, I noticed that I have to have both my nodejs server and my spring boot embedded tomcat server running (I used yarn start
to start the react app). Will this always be the case? Does the nodejs server need to be running in order to render the spring boot response into my reactjs UI? Or can I only have my tomcat server running.
Also, I noticed that in my react app it starts on localhost:3000, whereas, my spring boot app starts on localhost:9080. And in my package.json
I have proxy defined as localhost:8080
. How can I have both apps be under the same host and serve port?
Upvotes: 0
Views: 2080
Reputation: 6254
Follow my below steps and you would be good.
In your spring boot application under application/properties do not define any port so your sprig boot can run by default to 8080 or 9080 is fine if you are ok to run on it.
Create fat jar of your spring boot project
deploy it to /opt/your project name
on server.
run you boot app with nohup java -jar jarname &
or you create service for it read spring documentation to run your jar as service.
it will start your app on 8090
or 8080
whatever you want.
Now lets come to your front end app which is in react.
to order to run your front end app there are couple of ways.
apache/ngnix
root folder for ex /var/www/html
Now if you have lets say domain name example.com then your front end app can be accessible with http://www.example.com
and backend apis are available at http://www.example.com:8080
make sure 8080 and 80 are allowed.
Now you think,no i want to also want my apis to be accessible by www.example.com
then first make sure there context path is defined your apis so they available with base path lets say /api
is your base path.
then define proxy in apache or ngnix server.
<VirtualHost *:80>
ProxyRequests Off
ProxyPass /api http://localhost:8080/api
ProxyPassReverse /api http://localhost:8080/api
</VirtualHost>
Now you are good to also access your apis at www.example.com/api
Upvotes: 2
Reputation: 1267
@Robin, I made a project to my last company using the same technologies about 9 months ago. I put it in production before I leave and it was a successful! I have the boilerplate of this project https://github.com/gasparteixeira/springboot-reactjs-app.
Take a look at the structure. Example: For development keep using the spring boot tools and yarn start for reactjs! But when you're going to generate the jar/war be sure you have built (yarn run build) inside your react project.
Upvotes: 0