Python API server with angular front-end

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Python API server with angular front-end



It has been a while I built an app with Angularjs front-end and API back-end, so forgive me if this a pretty dumb question. I would like to run a Python back-end which serves an API. Every endpoint should start with /api/. Now this app runs in the folder 'api", this is somewhat my folder structure:


/api/


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



Now I am figuring out how I could run a webserver, which accesses the files from the api folder when I use an endpoint which starts with /api, and every other endpoint should access my AngularJS app which is available from the public folder. This way I can access the Python API endpoints from my AngularJS app and still be pretty secure because my Python source files are not available from the public folder.


/api



So, AngularJS endpoints:
/home
/login
/logout
etc.


/home


/login


/logout



Python API endpoints: /api/user, /api/user/profile etc. (every single endpoint which starts with /api/.


/api/user


/api/user/profile


/api/



Any ideas? I could run two servers on different ports, but that should not be the way to go. I've done something like this with PHP, but never done this with Python and somehow it gives me a headache at the moment.



If someone can help me out, that would be great.




1 Answer
1



You can use nginx to deploy this project.


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;





Thanks! Looks like a pretty good solution for this :)
– Erik van de Ven
Aug 10 at 10:21





Welcome. I think you should use a framework for the python server.
– argo
Aug 10 at 10:27





I do, I use Flask. I guess, what I did with PHP, was using PHP for the API and let PHP render the index.html on every non-api URL. The public folder contained one PHP file (index.php), which only loaded (require) a Bootstrap.php, which was located in the root folder. Perhaps, for development, I should be able to render the index.html on every non-api url, as default, so angular takes care of the url endpoints from there. That should work I guess.
– Erik van de Ven
Aug 10 at 10:46



require






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS