我有一个像这样的 Node 服务器:
var express = require('express');
var fs = require('fs');
var path = require('path');
var root = fs.realpathSync('.');
var app = express();
app.configure(function () {
app.use(express.static(__dirname + '/./'));
app.use(app.router);
});
app.get('/*', function (req, res) {
res.sendfile(path.join(root, './index.html'))
});
/*app.get('/dashboard', function (req, res) {
res.sendfile(path.join(root, 'index.html'))
});*/
app.listen(3000);
console.log('Listening on port 3000');
在前端,我使用主干路由通过 HTML History API 来路由应用程序模块。所以它始终是一个格式良好的 URL。当我刷新页面时就会出现这个问题。它在本地 Node 服务器中工作正常,但是当我在 Microsoft azure 云上部署应用程序时,刷新会产生问题,并显示找不到资源。
是否有任何特定于 azure 的内容需要我进行配置,以使其了解它应该在新请求上提供 index.html,而不是查找资源?
在 apache 中,我们使用 .htaccess 来执行此操作,但我不知道如何在 azure 云上执行相同的操作!
最佳答案
找到解决方案:
您需要在根目录中有一个 webconfig 文件才能使其工作。 Azure 内部只有 IIS 来提供文件服务。如果你仔细观察,一旦部署了应用程序,server.js 就没有任何用处了。 IIS 获得控制权来提供index.html 文件,因此您需要在IIS 配置中执行某些操作。以下是您需要放入应用程序根目录中的 Web.config(区分大小写)文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="redirect all requests" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="index.html" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
关于node.js - 在node.js 中为所有传入的http 请求提供index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26659777/