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

IIS通过HttpPlatformHandler部署Express访问报Cannot GET错误

IIS部署Express应用访问返回Cannot GET /nodetest/api/test排查

问题现象

  • 部署环境:IIS + HttpPlatformHandler 部署Node.js Express应用
  • 部署操作:在IISDefault Website节点下通过Add Application选项,添加别名为nodetest的Node应用
  • 访问表现:访问地址http://localhost/nodetest/api/test时,返回错误Cannot GET /nodetest/api/test

现有配置

入口文件app.js

const express = require('express')
const app = express()

const port = process.env.PORT || 3001;

app.get('/api/test', function(req, res){
    return res.status(200).send('First Server');
})

app.listen(port, () =>{
    console.log(`Server is listening to ${port}.....`);
})

web.config配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" requireAccess="Script" />
        </handlers>
        <httpPlatform stdoutLogEnabled="true" stdoutLogFile=".\node.log" startupTimeLimit="20" processPath="C:\Program Files\nodejs\node.exe" arguments=".\app.js">
            <environmentVariables>
                <environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
                <environmentVariable name="NODE_ENV" value="Production" />
            </environmentVariables>
        </httpPlatform>
    </system.webServer>
</configuration>

根因

HttpPlatformHandler默认会把IIS收到的完整请求路径直接转发给后端Node进程,当前IIS应用挂载在/nodetest虚拟路径下,转发给Express的请求路径为/nodetest/api/test,但Express代码里仅定义了/api/test的路由,没有匹配/nodetest前缀的路由规则,因此返回404类的GET方法不存在错误。

修复方案

二选一即可:

  • Express侧适配虚拟路径:给所有业务路由统一添加/nodetest前缀,和IIS虚拟路径保持一致,修改后app.js核心代码如下:
    const express = require('express')
    const app = express()
    const port = process.env.PORT || 3001;
    
    // 定义业务路由
    const bizRouter = express.Router();
    bizRouter.get('/api/test', function(req, res){
        return res.status(200).send('First Server');
    })
    // 挂载到和IIS虚拟应用一致的路径下
    app.use('/nodetest', bizRouter);
    
    app.listen(port, () =>{
        console.log(`Server is listening to ${port}.....`);
    })
    
  • IIS侧重写转发路径:安装IIS URL Rewrite模块后,在web.config中添加重写规则,转发前剥离路径中的/nodetest前缀,无需修改Node侧代码,新增配置位置为<system.webServer>节点下:
    <rewrite>
        <rules>
            <rule name="RemoveAppPrefix" stopProcessing="true">
                <match url="^nodetest/(.*)" />
                <action type="Rewrite" url="{R:1}" />
            </rule>
        </rules>
    </rewrite>
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 21:36:16