Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate back-end and front-end apps on same domain?

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)

like image 773
Erich Avatar asked Jul 31 '13 20:07

Erich


People also ask

Can I host frontend and backend on same server?

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.

Should backend and frontend be separated?

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.

How do you connect front end and back end in Web development?

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!

Do apps have a front and back end?

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.


2 Answers

You are gonna to dig yourself... deep :)

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:

  • single deployment (let's say to Heroku)
  • name space managed from one app
  • No need to modify the models in many apps after change in one of them

else if

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.

else

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

like image 84
biesior Avatar answered Sep 29 '22 07:09

biesior


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 :)

like image 41
Mahdieh Shavandi Avatar answered Sep 29 '22 07:09

Mahdieh Shavandi