【发布时间】:2015-12-08 09:34:20
【问题描述】:
我有一个基于 Node.JS 构建的 AngularJS 应用程序,它托管在 Azure (IIS) 中并使用 HTML5 模式。在我不使用 Node.JS 而只使用 AngularJS 和 IIS 的情况下,我可以使用 IIS Web.config 中的以下规则重写 URL,以便页面刷新不会导致 404 错误:
<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 之上使用 Angular(使用 ExpressJS)时,即使在 IIS 中使用上述规则,我也会在页面刷新时收到诸如“Cannot GET /myroute”之类的错误。我需要在 Server.js 中配置什么或在 Web.config 中配置不同的规则。
这是我目前在 Express 中的内容。我正在使用 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 什么也不做。
-
你有如何做到这一点的例子吗?
-
我没有,但它通常只是一个简单的 app.get( 中间件,它会跟踪您的路由并呈现 index.html 而不是给出 404。
标签: angularjs node.js iis url-rewriting iisnode