我正在使用Django+DRF
,CELERY+REDIS
,ReactJs+REDUX
和JWT
运行一个应用程序,并且我很难将backend
与frontend
连接以进行部署。
我已使用create-react-app
生成React代码;和npm run build
生成生产版本。
我一直在环顾四周,但找不到如何将它们链接在一起的教程。
如果你们口袋里有我可以追踪的任何链接,我会很感激您的。
您无需将React文件“移入” Django。只需单独为其服务。
这就是我为我的项目提供服务的方式:
我从/api/
网址提供我的API,所以这是我的Nginx配置的粗略表示:
server {
# urls that start with /api or /admin
location ~ ^/(api|admin) {
# pass them to uwsgi or gunicorn
...
}
# urls that start with /api or /admin
location /media {
# serve from your project's media folder
...
}
# urls that start with /static/admin
location /static/admin {
# these requests are made to admin's static files
# serve them from your staticroot folder
# i.e. where your files are stored after you run `collectstatic`
...
}
# everything else
location / {
# serve from frontend build folder
root /path/to/the/build/folder;
index index.html;
# this is important
# first try to find files matching url, like css, or js, or favicon
# if nothing found, serve the index.html
try_files $uri /index.html;
}
}
正如您在评论中提到的,这种解决方案对我来说似乎是合理的。
Deploying Seprate React Frontend and Django DRF API
就我所知,您可以在随机端口上启动Django + DRF的DEV版本,例如localhost:5000 /。然后,您可以在其他端口上的create-react-app上启动DEV服务器,例如localhost:8080 /。为了进行测试,您可以使用React应用程序中的DRF网址检查前端和后端之间的连接。
[之后,您可以构建自己的React应用,并将bundle.js作为标准js文件添加到Django项目中的index.html。在Django中创建'home'网址,并将其与简单模板连接,例如您在React应用程序的index.html中使用的模板。
您的bundle.js将包含所有必需的库。但是对于服务器上的生产版本,您将需要再次为具有实际域的实际URL构建应用程序。
希望有帮助。