r/javahelp • • 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?

24 Upvotes

21 comments sorted by

View all comments

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.