2013-05-03 158 views
1

是否有任何好的示例或指導,任何人都可以提供構建這樣的應用程序?node.js angular jade客戶端和node.js rest api

Client (client.company.com) 
    Node.js 
    Angular 
    Jade 
    ExpressJS 

Server (private) (server.company.com) 
    node.js 
    "rest" api (express) 

該API現在是私有的,只能從託管服務器訪問。

如果有一個頁面創建食譜例如,是這樣嗎? 客戶端

- angular form with router that posts to client.company.com/recipe 
- express would need route to handle that /recipe 
- that route would then post to api server server.company.com/recipe 
- then response would be propagated through the layers back to the ui. 

那是不是有客戶端複製api路由?有什麼可以做的,以簡化和減少重複的東西?

回答

2

角形應該直接發佈到api服務器。 Express僅用於提供角度html/javascript /靜態文件。 html和api之間的層數越少越好。我沒有看到有什麼好的理由說明爲什麼你需要客戶端來複制api路由。

由於您的api位於託管服務器後面,因此您可以設置nginx服務器將所有api調用從託管服務器路由到api服務器。以下是一個示例nginx的配置做路由:

upstream clientServer { 
    server client.company.com:80; 
} 

upstream apiServer { 
    server server.company.com:80; 
} 

server { 

    location/{ 
     root html; 
     index index.html index.htm; 
     proxy_pass     http://clientServer; 
     proxy_set_header   Host   $host; 
     proxy_set_header   X-Real-IP  $remote_addr; 
     proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for; 
    } 

    location /api { 
     proxy_pass     http://apiServer; 
     proxy_set_header   Host   $host; 
     proxy_set_header   X-Real-IP  $remote_addr; 
     proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for; 
    } 

注意上面是nginx.conf的一個片段。

Nginx會查看你的URL路徑。

  • 請求訪問/路徑會去客戶端服務器(在那裏你可以承載快遞JS和角文件)
  • 請求訪問/ API/*路徑將被轉發到API服務器

你的角表單可以直接調用api到/ api/*

希望有幫助。

+0

感謝您的回答。我們還沒有準備好這麼做,因爲api上沒有任何安全措施可以讓任何人使用它。 – dre 2013-05-07 23:29:54

+1

好了,那你上面說的是在正確的軌道上,除了你可以通過在api服務器上快速設置通配符路由來節省時間。如果需要身份驗證,則可以在express上截取請求,並在轉發到api服務器之前將必要的身份驗證令牌/等附加到http標頭/ url。 – maethorr 2013-05-08 08:43:27