簡體   English   中英

node.js角玉客戶端和node.js rest api

[英]node.js angular jade client and 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路由是否正確? 有什么方法可以簡化並減少重復工作?

角形應該直接發布到api服務器。 Express僅用於提供有角度的html / javascript / static文件。 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路徑。

  • 請求訪問/路徑將轉到客戶端服務器(您可以在其中托管express js和angular文件)
  • 訪問/ api / *路徑的請求將轉發到apiserver

然后,您的角度形式可以直接將api調用到/ api / *

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM