我使用Django
和制作了一些应用程序React.js
,对我来说最好的方法如下:
- “经典”后端
django-rest-framework
和适合您的数据库,
django
从前端通过API调用后端。
我的项目的经典架构如下所示:
.
└── your_ambitious_project
├── frontend
│ ├── public
│ │ ├── favicon.ico
│ │ ├── index.html
│ │ └── manifest.json
│ ├── src
│ ├── package.json
│ └── ...
├── backend_api
│ ├── core_api
│ │ ├── models.py
│ │ ├── views.py
│ │ └── ...
│ └── backend_api
│ ├── __init__.py
│ ├── settings.py
│ └── ...
└── ...
假设您要使用Nginx
,目的是:
- 使您
backend_api
的React.js
前端应用程序可以访问,
- 使用户能够访问您的
frontend/public/index.html
.
对我来说,一个典型的nginx.conf
文件看起来像这样:
upstream backend_api { # so we can access django backend api from frontend
server backend_api:8000;
}
# below: in dev environment only (for hot reloading)
# upstream frontend {
# server frontend:1337;
#}
server {
listen 80;
server_name your_ambitious_project;
location /api/ {
proxy_pass http://backend_api;
}
location / {
alias /var/www/frontend/build/; # for production environment
# proxy_pass http://frontend; # for dev environment (hot reloading)
}
}
然后,您必须使您的后端 api 可从外部访问,因此您将使用例如gunicorn
:
gunicorn siapi.wsgi:application --bind 0.0.0.0:8000
对于前端,在生产环境Nginx
中将在合适的位置(运行后npm build
)获得您的项目的构建,否则在开发环境中您将必须运行您的服务器:npm start
。
您可以从一些链接中获得灵感,就像我一样:
希望它可以帮助您了解如何结合使用这两个框架!