We are building a fully RESTful back-end with the Play Framework. We are also building a separate web front-end with a different technology stack that will call the RESTful API.
How do we deploy both apps so they have the same domain name, with some url's used for the backend API and some for the front-end views?
For example, visiting MyDomain.com means the front-end displays the home page, but sending a GET to MyDomain.com/product/24 means the back-end returns a JSON object with the product information. A further possibility is if a web browser views MyDomain.com/product/24, then the front-end displays an HTML page, and that webpage was built from a back-end call to the same url.
Finally, do we need two dedicated servers for this? Or can the front-end and back-end be deployed on the same server (e.g. OpenShift, Heroku)
You can't start both on the same one and if they are apart, it's impossible for the frontend code to access the backend endpoints. Fear not. If you really want to run both servers, you have to make sure that both are accessible through the same port.
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.
Learn about client-side and server-side rendering and create REST API endpoints to connect the front-end to the back-end. Get an introduction to the Model, View, Controller design pattern and create full-stack apps using MVC architecture. You've completed the Connecting Front-End to Back-End course!
In most web sites and web apps, you work with both ends. The same is true for mobile apps by the way. But theoretically, you can build web or mobile apps that only have a frontend.
Simplest and most clean approach with no any doubt is creating a single application serving data for both, BE and FE, where you differ response (JSON vs HTML) by the URL, pseudo routes:
GET /products/:id controllers.Frontend.productHtml(id) GET /backend/products/:id controllers.Backend.productJson(id)
Benefits:
If you're really determined to create a two separate apps, use some HTTP server as a proxy - for an example nginx
- so it will send all requests to domain.tld/*
to application working at port 9000
(which will answer with HTML) but requests to domain.tld/backend/*
redirect to application working at port 9001
responding with JSON.
If you are really gonna to response with JSON or HTML depending on the caller you can try to compare headers to check if request was sent from browser or from AJAX call in each controller , but believe me that will become a nightmare faster than you thing... insert the coin, choose the flavor
I thought of a different solution. I'm going to deploy back-end to a subdomain like
http://api.myapp.com
and deploy front-end to the main domain:
http://myapp.com/
but I think you'd better use 2 different hosts, one for front-end and one for back-end (I searched the google and this was the result of my investigations :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With