javascript - 单页应用程序的真实 URLS?

标签 javascript django reactjs

我在我的一个项目中使用了 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/

相关文章:

python - 在 Django 的 html 页面中渲染多个查询集

reactjs - 在另一个组件 V6 中 react 路由

javascript - React - 未找到模块,对于已安装包内的包,当需要 Express 时

javascript - 使用 Object.keys 获取用于创建构造函数实例的属性,然后打印继承的属性

javascript - 将另一个对象推送到数组中

javascript - 使用 Fluentlenium 在 dropzone.js 中上传文件

javascript - 如何应用可切换的主题,如推特?

在 Mac 上使用 Apache 和 mod_wsgi 设置 Django : configuration problems

Django:在syncdb上强制表创建顺序

reactjs - 基于全局状态变化重新渲染单个子组件而不重新渲染兄弟组件