Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve a decoupled React frontend?

So I am creating a multiplatform application using React & Spring (Java)

I want to make the Spring backend a REST webservice that can be called by both a React-Native and a React frontend.

I currently have my project broken up into 3 sub projects: backend (Spring), webapp-frontend (React webapp), mobile-frontend (React native)

My question is on how I should actually serve the React webapp frontend. I will have the webservice on a server somewhere so that the React code can hit it to make API calls, but as far as serving the React webapp would it be better to do serve it with the same backend server or would it be better to make a seperate frontend server with something like express? Also, are there any other alternatives?

like image 273
dszopa Avatar asked Mar 11 '23 03:03

dszopa


1 Answers

Serving the react webapp with either express or the same server as the REST backend is both valid options. I would say that if there is no specific reason for choosing express, serving it on the same server as the spring backend is your best choice.

Recall that the frontend and backend will still be decoupled and only communicate through the REST-interface so to extend the backend-server to host the react-webapp should be quite simple, here's an example that could be helful: React,Spring,Rest example

However there is a couple arguments that I see that could make you choose a separate server then the REST backend, for example:

  • Isolated restarts and failures. If you want to be able to restart only your react-frontend without affecting the REST backend you will probably want to run them on different servers. Another thing to consider is if you expect that the backend will need to scale heavily and you'll need to replicate the backend across multiple servers, that might be easier to do if the frontend is running on its own separate server.

  • Server-side rendering of JavaScript (React) is possible through a node.js server like express but I don't think it is possible with a Spring server.

Also, are there any other alternatives?

Other alternatives then express/Spring, Yes. Other alternatives then hosting the fronted on the backend-server or on its own server? No

There are pros and cons with both choices and what is best depends on your needs.

like image 100
Limmen Avatar answered Mar 19 '23 18:03

Limmen