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

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

Leave a Comment