node.js - 在node.js 中为所有传入的http 请求提供index.html

标签 node.js azure backbone.js

我有一个像这样的 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/

相关文章:

node.js - 微软广告 B2C 问题 : policy is missing

javascript - 在树遍历完成回调后执行函数

azure - Azure 负载均衡器如何支持 Service Fabric 集群的动态端口?

javascript - Backbone.js - 排序和迭代 - 比较器不起作用

node.js - 如何使用多个嵌套的私有(private) Node 模块?

azure - 我可以通过 Databricks 将数据提取到 azure 数据资源管理器中的表中吗?

azure - 首先在 Azure 上使用 EF Code 创建数据库时出错

javascript - Backbone.js 'constructor.prototype' 和 '__proto__' 之间的区别?

javascript - 为什么模型中嵌套了集合的多个副本?

node.js - NestJS 使用默认记录器或像 winston 这样的 npm 记录文件、数据库等