【问题标题】:Hosting node js website to Azure. Default path / not working将节点 js 网站托管到 Azure。默认路径/不工作
【发布时间】:2021-05-13 23:07:27
【问题描述】:

我有一个节点 js 网站,我正在使用 Visual Studio 代码将其部署到 azure 服务器。以下是我在 server.js 的代码

var express =require('express');
var bodyParser=require("body-parser")
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var nodemailer = require("nodemailer");
var port=process.env.PORT || 3000;

app.use(express.static(__dirname + '/public'));


app.get('/',function(req,res){

console.log('hello from server');

 res.render('./public/index.html');

  });

我将网站部署到 azure 应用服务,并且 url 总是希望将 index.html 添加到它的工作。例如 https://abc.azurewebsites.net 不起作用,但 https://abc.azurewebsites.net/index.html 起作用。如何从 url 中删除 index.html?

我的 azure 文件夹结构是 site/wwwroot/public/all html files。

谁能提出解决方案

【问题讨论】:

  • 基于window os还是linux os?
  • 基于windows操作系统

标签: node.js azure visual-studio-code


【解决方案1】:

更新:

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <webSocket enabled="false" />
    
    <rewrite>
      <rules>
        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
          </conditions>
          <action type="Rewrite" url="public/index.html"/>
        </rule>
      </rules>
    </rewrite>
    
    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin"/>
        </hiddenSegments>
      </requestFiltering>
    </security>
    <httpErrors existingResponse="PassThrough" />
  </system.webServer>
</configuration>

原答案:

只需在根目录('/site/wwwroot')中添加一个名为.htaccess的文件,这就是文件的内容:

DirectoryIndex public/index.html

之后,您的网络应用程序的默认设置将更改为“public/index.html”。

【讨论】:

  • 试过 .htaccess 不工作。我们必须在 web.config 中设置它吗?
  • @Philomath 我已经更新了 web.config。你可以谷歌“重写”web.config(这应该不需要通过代码来实现。)。
猜你喜欢
  • 2023-01-12
  • 1970-01-01
  • 1970-01-01
  • 2016-03-09
  • 2018-07-03
  • 2015-04-25
  • 2020-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多