繁体   English   中英

是否可以在同一个 Azure AppService 实例上执行 C# API 后端和 Node.JS 前端

[英]Is it possible to execute a C# API backend and Node.JS frontend on the same Azure AppService instance

我们有一个在 Node.JS 服务器之外运行 React 服务器的前端,该服务器与作为 a.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 App Service 中,我们正在运行.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"}

而对任何其他 URL 的调用而不是 /api /api2 给出:

{"type":"https://httpstatuses.com/404","title":"Not Found","status":404,"traceId":"xxx"}

我们在服务器日志中没有看到任何错误。

On Windows OS, IIS enables you to set up many virtual applications inside of a single website if you select the Windows Web App option. 要使用此策略,您可以遵循 Microsoft 提供的单个 azure 应用服务中的多个应用程序部署

注意:同一个应用程序池将由多个虚拟应用程序共享。

或者它可能是 app.js 后端文件中的 CORS 未启用广告正在停止通过前端访问后端。

如果有助于解决问题,您可以尝试以下步骤:

  1. 为 Angular、React、Node JS 等前端应用设置单独的应用服务。
  2. 从前端应用程序调用 Node JS API。

或者,

gulp文件用于在同一台服务器上搭建和操作客户端和服务器,然后将项目部署到应用服务。 在这种情况下,不会发生跨站点通信。 这是最受青睐的一种。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM