我用 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 做过这件事,不知何故,它现在让我很头疼。
如果有人可以帮助我,那就太好了。
你可以使用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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句