简体   繁体   English

配置sails服务器以使用html5Mode?

[英]configure sails server to work with html5Mode?

I have an angular-sails app where I want to remove the hash from urls 我有一个angular-sails应用程序,我想从网址中删除哈希

I added this to my client app.config 我将此添加到我的客户端app.config

$locationProvider.html5Mode(true);

and the routing works until I refresh the page. 并且路由工作直到我刷新页面。

navigating to http://localhost:1337/about works. 导航到http://localhost:1337/about工作。 I refresh and get 我刷新并得到

{
  "status": 404
}

from sails server. 来自sails服务器。 How do I configure sails to work with angularjs html5Mode? 如何配置sails以使用angularjs html5Mode?

To use HTML5Mode you need your server setup to only provide the your root (or "index.html") file. 要使用HTML5Mode,您需要将服务器设置为仅提供根(或“index.html”)文件。 "web.com/index.html". “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. 在我看来,最好将分离你的Sails应用程序和你的Angular前端分开交付。 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. 这样,您可以轻松设置为角客户端前端提供服务的Web服务器,以便仅在非AJAX调用上提供index.html。

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 您必须确保angular设置以下标头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();
};

This is a slightly different question, but I have a previous SO answer that may apply. 这是一个稍微不同的问题,但我之前的SO答案可能适用。 Page not found when refreshed on SailsJS 在SailsJS上刷新时找不到页面

You're running into this problem because when you reload the page, sails is responding, and it knows nothing about your angular routes. 您遇到了这个问题,因为当您重新加载页面时,风帆正在响应,并且它对您的角度路线一无所知。

You can set up a wildcard route to a controller method that will send down your single page app, or actual assets if they exist. 您可以设置一个通配符路由到一个控制器方法,该方法将发送您的单页应用程序,或实际资产(如果存在)。 It'll be less of a problem if you have a clear URL prefix, like /admin in my case. 如果你有一个清晰的URL前缀,就像我的情况下的/admin一样,那么问题就不那么严重了。 Here's what I'm using: 这是我正在使用的:

routes.js: routes.js:

'GET /admin/*': 'AdminController.index'

AdminController.js: AdminController.js:

var path = require('path');
var fs = require('fs');

module.exports = {
    index: function(req, res) {
        var requestedPath = req.url;
        var resolvedPath = path.resolve(requestedPath);
        if (resolvedPath.indexOf('/admin') !== 0) {
            res.send(403);
        } else {
            var publicRoot = sails.config.paths.public;
            var fullPath = path.join(publicRoot, resolvedPath);
            fs.exists(fullPath, function(exists) {
                if (exists) {
                    res.sendfile(resolvedPath, { root: publicRoot });
                } else {
                    res.sendfile('/admin/index.html', { root: publicRoot });
                }
            });
        }
    }
};

If the asset actually exists under the public area (like a .js file), send it down; 如果资产实际存在于公共区域(如.js文件)下,则将其发送.js ; otherwise, send down index.html . 否则,发送index.html With some paranoid path checks. 有一些偏执路径检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM