gpt4 book ai didi

Node.js Angular Jade 客户端和 Node.js Rest API

转载 作者:太空宇宙 更新时间:2023-11-03 23:44:25 25 4
gpt4 key购买 nike

是否有人可以提供任何好的示例或指导来构建这样的应用程序?

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

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

该 API 目前是私有(private)的,只能从托管服务器访问。

例如,如果有一个页面可以创建菜谱,这是对的吗?客户端

- 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 路由是否正确?有什么办法可以简化并减少重复吗?

最佳答案

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 的片段。

Nginx 将查看您的 URL 路径。

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

您的 Angular 形式可以直接调用 api 到/api/*

希望有帮助。

关于Node.js Angular Jade 客户端和 Node.js Rest API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16364681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com