【发布时间】:2021-08-20 17:36:57
【问题描述】:
我们有一个在 Node.JS 服务器之外运行 React 服务器的前端,该服务器与作为 .Net 5 Web 服务运行的后端通信。
服务在两个 Azure App Service 实例上运行良好,但是,我们希望通过让这两个实例在同一个 App Server 实例上运行来简化部署。
这可能吗,还是我们应该继续前进?
在我们的努力中,我们将以下 web.config 基于合并我们发现的不同想法拼凑在一起。它基本上是通过 iisnode 为 Node.JS 提供服务,而 /api 调用被定向到 .Net DLL。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<webSocket enabled="false" />
<handlers>
<remove name="aspNetCore"/>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
<add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
</handlers>
<rewrite>
<rules>
<!-- Serve static files directly -->
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}"/>
</rule>
<!-- All calls not going to "/api" or "/apiv2" are sent to server.js -->
<rule name="DynamicContent">
<match url="(api|apiv2)" negate="true" />
<action type="Rewrite" url="server.js"/>
</rule>
</rules>
</rewrite>
<aspNetCore processPath="dotnet" arguments=".\Backend.Api.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess"/>
</system.webServer>
</configuration>
我们将 server.js 更新为以下简单设置以避免任何额外问题;
const express = require('express');
const server = express();
// We need to get the port that IISNode passes into us
// using the PORT environment variable, if it isn't set use a default value
const port = process.env.PORT || 3000;
// Setup a route at the index of our app
server.get('/', (req, res) => {
return res.status(200).send('Hello World');
});
server.listen(port, () => {
console.log(`Listening on ${port}`);
});
在我们的 Azure 应用服务中,我们正在运行 .Net Stack 和 .Net 5 主要和次要版本。我们没有启动命令。应用服务计划正在运行 Linux。
总而言之,API 工作正常,是 iisnode/Node.JS 服务器没有按预期工作,出现 404 错误。我们假设完整的 Node.JS 甚至还没有启动。
调用路由会给出如下错误消息:
{"type":"https://httpstatuses.com/405","title":"Method Not Allowed","status":405,"traceId":"xxx"}
而对 /api /api2 以外的任何其他 URL 的调用:
{"type":"https://httpstatuses.com/404","title":"Not Found","status":404,"traceId":"xxx"}
我们在服务器日志中没有看到任何错误。
【问题讨论】:
标签: c# node.js express azure-web-app-service iisnode