r/javahelp • u/RPauly13 • Aug 08 '20
Spring boot backend/React frontend: is it good/common practice to run frontend and backend on the same domain?
This may be a stupid question. I’m fairly new to actually thinking about the design of my web applications.
Is it common practice/smart to build my web app in a way that spring boot would be running on localhost:8080 and my frontend(made with React) running on the same port?
From there I could hit my Rest controller at localhost:8080/api and my homepage at localhost:8080/home
Bonus question: where should I start when I have more questions like this?
5
u/iamsooldithurts Aug 08 '20
We are doing something similar with Vue currently. Users hit the front end endpoints and the front end hits the API/backend end points as necessary.
You can serve them separately, you just have to configure it all properly. We should be doing so, but our infrastructure has not caught up to that level of architecture.
But, during development, being able to run “npm run serve” and then edit pages while they’re hot massively improves development time efficiency. It proxies requests to the real backend running in another window, or the background, while rebuilding your pages on the fly; by the time you switch from the IDE to the browser it’s already reloaded the new page with the new code, or at least has a compile error waiting for you.
1
u/RPauly13 Aug 08 '20
Oh okay that’s pretty interesting. So they’re just on the same server, but different ports?
2
u/iamsooldithurts Aug 08 '20
Yeah, npm run serve grabs 8082 and redirects backend calls to 8080. But only for local use.
In prod, our Spring MVC (tomcat) serves up the front and back ends on the same port, this the need for different end points.
The major benefit IMNSHO is that you can serve the front end and backend on different (physical) servers, and deploy updates to the front end without affecting customers’ use of the API/backend.
We don’t have a public facing API/backend, and that’s why we haven’t taken that last step.
3
u/deathmetal27 Extreme Brewer Aug 08 '20
If you really want to do this, you could serve your react app in your web application. This way, your frontend and backend, both run on the same port. I have seen some applications do this and the advantage here is that you don't have to deal with CORS issues.
Though I have also seen lots of projects where the front end and back end run as separate applications. In such cases, its best to use a reverse proxy to ensure that both your front end and back end are on the same domain to avoid CORS related issues.
1
u/RPauly13 Aug 08 '20
Thanks! I’ll look into reverse proxies too because there seems to be an advantage, in terms of scaling and making changes, that comes with running them separately
2
1
u/Python4fun Aug 08 '20
There will be a Tomcat container inside the Springboot application that will register itself as handler for that port, and then the port won't be available for another process to use. You can use 8081 or 8090 for the backend. It's usually best to keep your front-end on standard ports.
2
1
u/yogsma Aug 08 '20
In my experience, I have not seen both backend and frontend running on the same port.
Usually by default, if you run react frontend, it will run on port 3000 and backend (Spring Boot - embedded jetty or tomcat) on port 8080.
With react frontend, you can add a routing module to proxy your request from 3000 to 8080 for connecting to RestController.
Adding my blog post https://betterjavacode.com/programming/how-to-create-an-application-with-spring-boot-and-reactjs for more information.
1
u/RPauly13 Aug 08 '20
Thanks, I’ll check out that blog post. Seems helpful. Thanks for the response!
2
u/maifee Aug 08 '20
My suggestion is run them in different port.. running both on same port and putting the UI inside static folder in spring is something like spring MVC... I respect MVC a lot.. but I love boot.. why? Keep that for a different question... In spring MVC you would just return a spring and your server would look for that jsp, in static folder and render that, to make it different you need to configure a lots of things.. in spring boot you just make RESTing API, and let the UI call them... And it's a lot secure too.. now server will receive request from UI server(maybe in same server, just different port)... And UI can be accessed from anywhere,.. so it's more secure, if you receive request from different IP, just block that on your UI to, there is many way to do so... Ask me if you want to clear anything..and yes there is many more things (benefits) of running them on different port..
2
u/ZeroGainZ Aug 08 '20
Sure, just add routing rules to the load balancer. Forward /api to your back end. Others routes to your front end.
2
1
u/puspendert Aug 08 '20
Assuming you spring boot backend is a rest api.
I am running my application (same stack as above) onto a single aws instance. It's easy to do. Run your spring boot jar at 8080 and react application at 3000. Set-up tomcat web server and reverse proxy for both the ports.
Run your spring boot api at localhost:8080/api
And react at localhost:3000
Now setup tomcat proxy such at all calls to /api will be forwarded to 8080 port and all other calls to 3000
11
u/sbhandari Aug 08 '20
In a same server, you cannot have two application listening in a same port. If you have containerized app, then they can listen on their 8080 port on its own container but has to be mapped with different/unique host port.
To achieve what you want, you can bundle springboot and react on same app as single app. Now request on 8080 will come to this app, and how you want to process that request relies on your app logic. And that is ofcourse very common for monolith apps to have UI integrated in the app itself.