在同一个域中分离后端和前端应用程序吗? [英] Separate back-end and front-end apps on same domain?

查看:334
本文介绍了在同一个域中分离后端和前端应用程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在构建一个完全基于REST的后端使用Play框架。我们也正在建立一个单独的Web前端使用不同的技术堆栈,将调用REST风格的API。

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.

我们如何部署这两个应用,使他们具有相同的域名,与用于后端API某些URL的有的为前端看法?

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?

例如,参观MyDomain.com意味着前端显示主页,但发送一个GET来MyDomain.com/product/24意味着后端返回产品信息的JSON对象。另一种可能性是,如果一个Web浏览器中查看MyDomain.com/product/24,那么前端显示一个HTML页面,该网页是从后端调用相同的URL建。

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.

最后,我们需要为这两个专用的服务器?或者可以在前端和后端部署在同一台服务器上(例如OpenShift,Heroku的)

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)

推荐答案

简单,没有任何疑问,最干净的方法是创建两个数据提供一个单一的应用,通过URL来和FE,在那里你不同的响应(JSON VS HTML),伪线路:

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)

好处:


  • 单个部署(假设为Heroku的)

  • 命名空间从一个应用程序管理

  • 无需修改变更后许多应用程序模型在其中的一个

如果你真的立志打造一个两个单独的应用程序,使用一些HTTP服务器作为代理 - 为例 nginx的 - 因此它会发送所有请求 domain.tld / * 应用程序端口<$ C $工作C> 9000 (将与HTML回答),但要求 domain.tld /后端/ * 重定向到应用程序在港工作 9001 使用JSON响应。

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.

如果你真的要来响应与根据呼叫者JSON或HTML你可以尝试比较头,检查是否请求从浏览器或从每个控制器AJAX调用发送,但请相信我,这将成为一场噩梦的速度比你的事情...插入硬币,选择香味

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

这篇关于在同一个域中分离后端和前端应用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆