2015-05-23 108 views
0

我已经在用户路由中删除了(#)hashbangs $locationProvider.html5Mode(true)。 并设置<base href="/public/">删除(#)从angular-ui路由器中的路由hashbangs后发出的问题

当我试图localhost:59940/public/#/home url#是删除和url看看localhost:59940/public/home和获取主视图。

但是当我尝试localhost:59940/public/home的网址。我收到404 Not Found错误。

帮助我在浏览器中尝试访问localhost:59940/public/home时获取主页视图。

回答

1

问题是浏览器在您的角度应用响应之前解析urls,并且确实该路由的资源不存在。我使用的解决方案是让你的404路由返回index.html页面,并使用ui-router处理真实的404情况。但另一个想法是将所有客户端路由匹配到服务器上返回index.html的路由。

1

要启用$locationProvider.html5Mode角度,您还需要对服务器进行一些更改。

其他则是您的静态资产和apis路径,所有其他路线应该只服务器index.html(您的主SPA页面)。

请参阅下面的代码。 这是你如何使用express服务器在node.js中完成的。

var app = require('express')(); 
 

 
app.configure(function() { 
 

 
    // Static files - all js, css, images, etc go into the static path 
 
    app.use('/static', express.static('/static')); 
 

 
    // If a static file is invalid so we send 404 
 
    app.use('/static', function(req, res, next) { 
 
    res.send(404); 
 
    }); 
 

 
    // This route deals enables HTML5Mode by forwarding missing files to the index.html 
 
    app.all('/*', function(req, res) { 
 
    res.sendfile('index.html'); 
 
    }); 
 

 
}); 
 

 
app.listen(3000);