我有一個基於Azure(IIS)託管並使用HTML5模式的Node.JS上構建的AngularJS應用程序。在情況下,我不使用Node.js的,只是使用與IIS AngularJS我可以重寫URL,這樣使用在IIS Web.config中的以下規則刷新頁面不會導致404錯誤:在AngularJS中使用適用於角度HTML5模式的節點重寫IIS URL
<rule name="AngularJSHTML5Mode" stopProcessing="true">
<match url=".*"/>
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true"/>
</conditions>
<action type="Rewrite" url="/"/>
</rule>
但是,在Node.js(使用ExpressJS)的頂部使用Angular時,即使在IIS中使用上述規則,也會在頁面刷新時出現錯誤,例如「Can not GET/myroute」。有什麼我需要在Server.js中配置或Web.config中的不同規則。
以下是我目前在Express中的內容。我使用的快遞4:
// Initialize Express App
var app = express();
var listener = app.listen(1337, function() {
console.log('Listening on port ' + listener.address().port); //Listening on port 1337
});
app.use(express.static(__dirname + "/public"));
據我瞭解,頁面刷新不應該在一個SPA作爲一個實踐問題發生,但他們這樣做,我需要考慮到這一點。
不,你必須在node.js中配置它。一旦請求被傳遞給node.js,web.config將不執行任何操作。 –
你有一個如何做到這一點的例子嗎? – Kode
我不這樣做,但它通常只是一個簡單的app.get(追蹤你的路線並呈現index.html而不是給出404的中間件) –