【问题标题】:Python API server with angular front-end带有角度前端的 Python API 服务器
【发布时间】:2018-08-10 09:13:43
【问题描述】:

我用 Angularjs 前端和 API 后端构建了一个应用程序已经有一段时间了,如果这是一个非常愚蠢的问题,请原谅我。我想运行一个提供 API 的 Python 后端。每个端点都应该以/api/ 开头。现在这个应用程序在“api”文件夹中运行,这有点像我的文件夹结构:

-api/
-- controllers/
-- __init__.py
-config/
-public/
-- assets/
-- index.html
-server.py

现在我正在研究如何运行网络服务器,当我使用以/api 开头的端点时,它会访问 api 文件夹中的文件,并且所有其他端点都应该访问我的 AngularJS 应用程序,该应用程序可从公众获得文件夹。通过这种方式,我可以从我的 AngularJS 应用程序访问 Python API 端点并且仍然非常安全,因为我的 Python 源文件在公共文件夹中不可用。

所以,AngularJS 端点: /home /login /logout 等等

Python API 端点:/api/user/api/user/profile 等(每个以 /api/ 开头的单个端点。

有什么想法吗?我可以在不同的端口上运行两台服务器,但这不应该是要走的路。我用 PHP 做过类似的事情,但从来没有用 Python 做过,不知何故这让我现在很头疼。

如果有人可以帮助我,那就太好了。

【问题讨论】:

    标签: python angularjs python-3.x api


    【解决方案1】:

    你可以使用nginx来部署这个项目。

    server {
        listen 80;
        listen [::]:80;
    
        server_name xyz.com;
    
        root /var/www/frontend/xyz-frontend/dist;
        index index.php index.html index.htm index.nginx-debian.html;
    
    
        # handles the api requests
        location /api {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                proxy_pass http://unix:/var/www/services/xyz/api.sock;
        }
    
        # FOR ANGULAR BUILD
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.html /custom_50x.html;
        }
    

    【讨论】:

    • 欢迎。我认为你应该为 python 服务器使用一个框架。
    • 我愿意,我使用 Flask。我想,我对 PHP 所做的就是将 PHP 用于 API,并让 PHP 在每个非 API URL 上呈现 index.html。公共文件夹包含一个 PHP 文件 (index.php),该文件仅加载 (require) 一个位于根文件夹中的 Bootstrap.php。也许,为了开发,我应该能够在每个非 api url 上呈现 index.html,默认情况下,因此 angular 会从那里处理 url 端点。我猜这应该可行。
    猜你喜欢
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2022-08-10
    • 2019-04-26
    相关资源
    最近更新 更多