我有一个角帆的应用程序,我想删除从URL中配置sails服务器以使用html5Mode?
我加入这个哈希我的客户app.config
$locationProvider.html5Mode(true);
和路由工作,直到我刷新页面。
导航至http://localhost:1337/about
工程。我刷新并获取
{
"status": 404
}
from sails server。如何配置sails以使用angularjs html5Mode?
我有一个角帆的应用程序,我想删除从URL中配置sails服务器以使用html5Mode?
我加入这个哈希我的客户app.config
$locationProvider.html5Mode(true);
和路由工作,直到我刷新页面。
导航至http://localhost:1337/about
工程。我刷新并获取
{
"status": 404
}
from sails server。如何配置sails以使用angularjs html5Mode?
这是一个稍微不同的问题,但我有一个以前的答案可能适用。 Page not found when refreshed on SailsJS
你遇到了这个问题,因为当你重新加载页面时,sails正在响应,并且它对你的角度路由一无所知。
要使用HTML5Mode,您需要将服务器设置为仅提供根(或“index.html”)文件。 “web.com/index.html”。在我看来,最好的做法是将您的Sails应用程序和您的Angular前端分开分开。通过这种方式,您可以轻松设置服务于角度客户端前端的Web服务器,只为非AJAX调用提供index.html。
要在sails中做到这一点,没有“开关”,你可以设置一个应用程序的广泛政策,检查角度是否打电话或浏览器正在发出呼叫并发送适当的文件,你可以看到下面的代码。你将不得不确保角度是设置以下头 X-要求,随着= 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();
};
您可以设置一个通配符路由到一个控制器方法,将发送了你的单页面应用程序或实际资产(如果存在)。如果你有一个明确的URL前缀,那么这个问题就不会那么严重了,例如我的案例中的/admin
。下面是我使用的是什么:
routes.js:
'GET /admin/*': 'AdminController.index'
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 });
}
});
}
}
};
如果资产的公共区域(如.js
文件)下确实存在,发送它下来;否则,请发送index.html
。随着一些偏执的路径检查。
我在这里有问题,当我重新加载任何页面(除(())我得到404。 – krekto
你有你的设置的基本示例,你可以发布的地方? – Meeker
我只是在角度上启用app.js的html5mode,添加了上面的警察,并启用“*”:isAjax;在政策。 – krekto