You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署Node.js至Azure App Service遇默认文档错误,Express路由已配置仍异常

解决Azure App Service部署Express.js应用后访问'/'路由报错的问题

问题原因

Azure App Service上的Node.js应用是通过IISNode托管的,默认情况下IIS会优先查找预设的默认文档(如index.html、index.js),找不到就会触发"无权查看此目录或页面"的错误,而不会自动将请求交给Express处理。

解决步骤

  • 添加web.config文件
    在项目根目录创建web.config文件,配置IIS将所有动态请求转发到你的Express入口文件(以下示例假设入口文件是server.js,请根据实际情况替换):

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.webServer>
        <handlers>
          <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
        </handlers>
        <rewrite>
          <rules>
            <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
              <match url="^server.js\/debug[\/]?" />
            </rule>
            <rule name="StaticContent">
              <action type="Rewrite" url="public{REQUEST_URI}"/>
            </rule>
            <rule name="DynamicContent">
              <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
              </conditions>
              <action type="Rewrite" url="server.js"/>
            </rule>
          </rules>
        </rewrite>
        <iisnode nodeProcessCommandLine="&quot;%programfiles%\nodejs\node.exe&quot;" />
      </system.webServer>
    </configuration>
    
  • 配置Azure启动命令
    登录Azure门户,进入你的应用服务 -> 配置 -> 常规设置,在"启动命令"栏填写node server.js(替换为你的实际入口文件名),确保IIS直接启动你的Express应用。

  • 验证Express路由配置
    确认你的Express代码中正确挂载了'/'路由,比如:

    const express = require('express');
    const app = express();
    const path = require('path');
    
    // 确保视图目录配置正确
    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'ejs'); // 或你使用的模板引擎
    
    app.get('/', (req, res) => {
      res.render('login'); // 渲染登录视图的逻辑
    });
    
    const port = process.env.PORT || 3000;
    app.listen(port, () => {
      console.log(`Server running on port ${port}`);
    });
    

内容的提问来源于stack exchange,提问作者Tyler Ewing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 16:03:39