配置帆服务器html5Mode工作? [英] configure sails server to work with html5Mode?

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

问题描述

我有一个角帆的应用程序,我想删除从URL中的散列

我说这个我的客户的app.config

  $ locationProvider.html5Mode(真);

和路由工作,直到我刷新页面。

导航到的http://本地主机:1337 /约的作品。我刷新并获得

  {
  状态:404
}

从帆服务器。如何配置与船帆angularjs html5Mode?工作

解决方案

要使用HTML5Mode你需要你的服务器设置为仅提供您的根(或的index.html)的文件。 web.com/index.html。在我看来这是最好的分离风帆App和您的角前端被seperatly交付完成。这样,你可以是您的服​​务客户的角度前端,可以轻松地设置您的Web服务器只会在非AJAX调用的index.html。

要做到这一点的帆没有开关,可以设置检查是否角正在通话或浏览器正在通话并发送相应的文件应用的策略,您可以看到code以下。你必须确保角度是设置下面的头
的X请求-随着= XMLHtt prequest

应用程序策略

  module.exports =功能(REQ,资源,下一个){  //检查,如果这是一个Ajax请求
  //如果真那么跳过此并返回请求的内容
  //如果为假,则返回指数(根)网页
  如果(如!req.isAjax){
    返回res.view(/指数);
  }  返回下一个();
};

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

I added this to my client app.config

$locationProvider.html5Mode(true);

and the routing works until I refresh the page.

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

{
  "status": 404
}

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

解决方案

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.

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

App Policy

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();
};

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

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