代码之家  ›  专栏  ›  技术社区  ›  Erik van de Ven

带角度前端的Python API服务器

  •  0
  • Erik van de Ven  · 技术社区  · 6 年前

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

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

    /api ,其他每个端点都应该访问我的AngularJS应用程序,该应用程序可从公用文件夹中获得。通过这种方式,我可以从我的AngularJS应用程序访问Python API端点,而且仍然非常安全,因为我的Python源文件无法从公共文件夹中获得。

    因此,AngularJS端点: /home /login /logout 等。

    Python API端点: /api/user , /api/user/profile /原料药/ .

    如果有人能帮我,那就太好了。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Arghya Saha    6 年前

    您可以使用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;
        }