2015-05-23 107 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);