配置sails 服务器以使用html5Mode? [英] configure sails server to work with html5Mode?

查看:24
本文介绍了配置sails 服务器以使用html5Mode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 angular-sails 应用程序,我想从 urls 中删除哈希

I have an angular-sails app where I want to remove the hash from urls

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

I added this to my client app.config

$locationProvider.html5Mode(true);

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

and the routing works until I refresh the page.

导航到 http://localhost:1337/about 有效.我刷新并得到

navigating to http://localhost:1337/about works. I refresh and get

{
  "status": 404
}

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

from sails server. How do I configure sails to work with angularjs html5Mode?

推荐答案

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

To use HTML5Mode you need your server setup to only provide the your root (or "index.html") file. "web.com/index.html". In my opinion this is best done by separating your Sails App and your Angular front end to be delivered seperatly. That way you can easily setup your web server that is serving your angular client front end, to only serve index.html on non AJAX calls.

要在sails中做到这一点,没有开关",你可以设置一个应用程序范围的策略来检查angular是在打电话还是浏览器在打电话并发送适当的文件,你可以看到下面的代码.您必须确保 angular 正在设置以下标题X-Requested-With = XMLHttpRequest

To do this in sails there is no "switch", you could set a app wide policy that checks if angular is making a call or the browser is making the call and send the appropriate file, you can see the code below. You would have to make sure angular is setting the following header 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();
};

这篇关于配置sails 服务器以使用html5Mode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆