我在我的一个项目中使用了 React Router,所以 React 是前端库,路由由 React Router 管理,后端 View 在 Django 中,API 在 Django Rest 中
所以我在浏览 react-router 文档时遇到了这个:-
Configuring Your Server Your server must be ready to handle real URLs. When the app first loads at / it will probably work, but as the user navigates around and then hits refresh at /accounts/23 your web server will get a request to /accounts/23. You will need it to handle that URL and include your JavaScript application in the response.**
我想知道这如何与 Django View 一起工作。
最佳答案
在开发服务器上,您只需为所有不以 api/
或 static/
开头的内容设置一个路由,以返回您的基本 app。 html
文件。示例
class AppHTMLView(View):
def get(self, request):
fn = os.path.join(settings.BASE_DIR, "app", "app.html")
with open(fn, 'r') as fh:
return HttpResponse(fh.read())
在您的生产服务器上,您相应地配置 Nginx。像这样
...
location / {
root /var/www/example.com/static_files/;
try_files '' /app.html =404;
}
但这并不是 React 特有的,而是所有单页应用程序所共有的。
关于javascript - 单页应用程序的真实 URLS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37719502/