Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single Page Application - Frontend independent of backend?

I've done some research and I've noticed that in a lot of examples Symfony2/AngularJS apps the frontend and backend are combined; for example, views use Twig.

I'd always thought that it's possible (and common practice) to create the frontend and backend separately and just join them by API. In that case if I want to change a PHP framework I will can do it without any problems and it will be enough to keep API.

So what are the best practices for doing it? It would be great if you could explain it to me and even greater if you just give me a link to good example on github or something.

like image 888
tdudzik Avatar asked Oct 04 '14 18:10

tdudzik


People also ask

Should frontend and backend be deployed separately?

Some companies like to have clear separation between the frontend code and the backend code. They go to the extent of having two different repositories - one for the backend and one for the frontend code. In this architecture the backend acts like a pure API. The backend returns just JSON data.

Is it good to separate frontend and backend?

With a separate frontend and backend, the chances of breaking the entire website are significantly low. It is also relatively easier to debug since it is clear from the start whether there is an issue in the frontend or backend. Upgrading your web applications makes them faster and reduces the risk of bounces.

Should frontend and backend be in separate repos?

If you can't come to a decision now, I would recommend that you start with separate repositories, because it is easier to merge repositories later on than it is to split them. Actually, with the right tools - e.g. git subtree , git filter-repo - both splitting and merging are pretty easy.

What is the difference between single page application and web application?

There are two general approaches to building web applications today: traditional web applications that perform most of the application logic on the server, and single-page applications (SPAs) that perform most of the user interface logic in a web browser, communicating with the web server primarily using web APIs.


1 Answers

We have been developing some projects using the same approach. Not only I think it doesn't have any "side effect", but the solution is very elegant too. We usually create the backend in Node.js, and it is just an API server (not necessarily entirely REST-compliant). We then create another, separate web application for the frontend, written entirely in HTML5/JavaScript (with or without Angular.js). The API server never returns any HTML, just JSON! Not even an index structure.

There are lots of benefits:

  • The code is very clean and elegant. Communication between the frontend and the backend follow standardized methods. The server exposes some API's, and the client can use them freely.
  • It makes it easier to have different teams for the frontend and the backend, and they can work quite freely without interfering with each other. Designers, which usually have limited coding skills, appreciate this too.
  • The frontend is just a static HTML5 app, so it can (and we often did) easily be hosted on a CDN. This means that your servers will never have to worry about static contents at all, and their load is reduced, saving you money. Users are happier too, as CDNs are usually very fast for them.

Some hints that I can give you based on our experience:

  • The biggest issue is with authentication of users. It's not particularly complicated, but you may want to implement authentication using for example protocols like OAuth 2.0 for your internal use. So, the frontend app will act as a OAuth client, and obtains an auth token from the backend. You may also want to consider moving the authentication server (with OAuth) on another separate resource from the API server.
  • If you host the webapp on a different hostname (e.g. a CDN) you may need to deal with CORS, and maybe JSONP.
  • The language you write the backend in is not really important. We have done that in PHP (including Laravel), even though we got the best results with using Node.js. For Node.js, we published our boilerplate on GitHub, based on RestifyJS

I asked some questions in the past you may be interested in:

  • Web service and API: "chicken or egg"
  • Security of an API server: login with password and sessions
like image 142
ItalyPaleAle Avatar answered Sep 28 '22 09:09

ItalyPaleAle