简体   繁体   中英

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.

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'

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.

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.

The browser I am testing against is IE9, does anyone know of a solution to this?

Have you tried setting the routes statically with express in your node server? For your route, in your server.js, it would look like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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