简体   繁体   English

深层路由无法正常工作的角度html5模式后备

[英]angular html5 mode fallback for deep routes not working

I have set up html5 mode in angular with node.js but the fallback to hash does not work for routes more than 1 level deep for browsers that do not support html5 mode. 我已经使用node.js在角度设置了html5模式,但是对于不支持html5模式的浏览器,哈希回退不适用于深度超过1级的路由。

Server route to catch all: 服务器路由捕获所有:

app.all('/*', function(req, res) {
    console.log('serve');
    res.sendFile(__dirname+'/public/index.html');
});

I have included a base tag right after the opening head tag. 我在开头标签之后紧接了一个基本标签。

<base href="/">

Now when i visit a url like 'localhost/news' 现在,当我访问类似“ localhost / news”的网址时

For supporting browsers the site loads at the same url and the server is hit once, logging 'serve', for non supported browsers it changes the url to localhost/#/news which is great. 为了支持浏览器,该站点将在相同的URL上加载,并且服务器将被命中一次,并记录“ serve”;对于不受支持的浏览器,它将URL更改为localhost /#/ news,这非常好。

Now for a url like localhost/news/post1 the supporting browser again works fine, but non supporting browsers load the index.html file, with no style or JS and the server is hit many times, logging serve well over 50+ times, the URL is not modified and the page remains broken. 现在,对于像localhost / news / post1这样的URL,支持的浏览器再次可以正常工作,但是不支持的浏览器可以加载index.html文件,没有样式或JS,并且服务器被击中了很多次,日志记录服务了50多次, URL未被修改,页面仍然损坏。

The browser I am testing against is IE9, does anyone know of a solution to this? 我要测试的浏览器是IE9,有人知道解决方案吗?

Have you tried setting the routes statically with express in your node server? 您是否尝试过在节点服务器中使用express静态设置路由? For your route, in your server.js, it would look like this: 对于您的路由,在server.js中,它看起来像这样:

app.use('/news', express.static(__dirname + '/public'));

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

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