【问题标题】:node.js angular jade client and node.js rest apinode.js angular玉客户端和node.js rest api
【发布时间】:2013-04-28 04:44:41
【问题描述】:

任何人都可以为构建这样的应用程序提供任何好的示例或指导吗?

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 路由是否正确?有什么可以做的来简化和减少重复的事情吗?

【问题讨论】:

    标签: node.js rest angularjs express pug


    【解决方案1】:

    angular 表单应该直接发布到 api 服务器。 Express 仅用于提供 angular 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 的 sn-p。

    Nginx 会查看你的 URL 路径。

    • 访问 / 路径的请求将转到客户端服务器(您可以在其中托管 express js 和 angular 文件)
    • 访问 /api/* 路径的请求将被转发到 apiserver

    然后你的 Angular 表单可以直接调用 api 到 /api/*

    希望对您有所帮助。

    【讨论】:

    • 感谢您的回答。我们还没有准备好这样做,因为 api 上没有任何人可以使用它的安全性。
    • 好的,那么你上面所说的一切都在正确的轨道上,除非你可以通过设置通配符路由到 api 服务器来节省时间。如果需要身份验证,您可以在 express 上拦截请求并将必要的身份验证令牌/等附加到 http 标头/url,然后再转发到 api 服务器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 2016-06-19
    • 2011-09-16
    相关资源
    最近更新 更多