javascript - 配置 sails 服务器以使用 html5Mode?

标签 javascript angularjs sails.js angular-ui-router

我有一个 angular-sails 应用程序,我想在其中从 url 中删除散列

我将此添加到我的客户端 app.config

$locationProvider.html5Mode(true);

路由一直有效,直到我刷新页面。

导航到 http://localhost:1337/about作品。我刷新得到

{
  "status": 404
}

来自风 sails 服务器。如何配置 sails 以使用 angularjs html5Mode?

最佳答案

要使用 HTML5Mode,您需要将服务器设置为仅提供您的根(或“index.html”)文件。 “web.com/index.html”。在我看来,最好将您的 Sails 应用程序和您的 Angular 前端分开以单独交付。这样你就可以轻松地设置为你的 Angular 客户端前端提供服务的 web 服务器,以便仅在非 AJAX 调用上提供 index.html。

要在 sails 中执行此操作没有“开关”,您可以设置一个应用程序范围的策略来检查是 angular 正在调用还是浏览器正在调用并发送适当的文件,您可以看到下面的代码。您必须确保 Angular 正在设置以下标题 X-Requested-With = XMLHttpRequest

应用政策

module.exports = function(req, res, next) {

  // Checks if this is an ajax request 
  // If True then skip this and return requested content
  // If False then return the index (or root) page  
  if (if !req.isAjax) {
    return res.view("/index");
  }

  return next();
};

关于javascript - 配置 sails 服务器以使用 html5Mode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24637800/

相关文章:

javascript - meteor 铁:router transitions

javascript - 单词搜索游戏。如何搜索网格并突出显示结果?

ruby-on-rails - 如何使用 angularjs 和 rails/devise 更新密码?

javascript - 在 AngularJS 中根据 $cookies 值确定路由模板的最佳方法是什么?

javascript - 执行条件 ng-if 检查的 "angular"方法是什么?

node.js - 一对一关联是否应该填充两侧?

javascript - 如何从 NPM 包中获取 Angular 5 应用程序环境变量?

javascript - jQuery - 观察元素高度变化

node.js - 使用app.use在sails.js中设置一些路线

node.js - "clean"在Sails.js 中使用不同ORM(例如node-orm2)的方法?