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

beN*_*erd 2 azure node.js backbone.js

我有一个像这样的节点服务器:

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');
Run Code Online (Sandbox Code Playgroud)

在前端,我使用骨干路由使用HTML History API路由应用程序模块.所以它始终是格式良好的URL.刷新页面时出现问题.它在本地节点服务器中工作正常,但是当我在微软Azure上部署应用程序时,刷新会产生问题,它说,它无法找到资源.

我需要配置哪些特定于azure的内容,以使其理解它应该在新请求上提供index.html而不是查找资源?

在apache中,我们使用.htaccess这样做,但我不知道如何在天蓝云上做同样的事情!

beN*_*erd 10

找到解决方案:

您需要在根目录中有一个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>
Run Code Online (Sandbox Code Playgroud)