【问题标题】:Is it possible to execute a C# API backend and Node.JS frontend on the same Azure AppService instance是否可以在同一个 Azure AppService 实例上执行 C# API 后端和 Node.JS 前端
【发布时间】: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


    【解决方案1】:

    如果您选择 Windows 操作系统上的 Web 应用程序,IIS 允许您在单个网站中配置多个虚拟应用程序。对于这种方法,您可以关注deploying of multiple applications in a single azure app service

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

    或者

    尝试在您的 nodejs 后端 app.js 文件中使用跨域资源共享 (CORS),这可能是阻止前端应用访问后端的原因。

    一般我们做的是:

    1. 在单独的应用服务上部署前端应用、react、angular 等。
    2. 并且,nodejs 应用在不同的应用服务上。
    3. 并且,从前端调用nodejs应用的api。

    或者,

    我们使用 gulp 文件在同一台服务器上设置和运行客户端和服务器,并将该项目部署在应用服务上。在这种情况下,没有跨站点通信。这是最喜欢的。

    【讨论】:

    • 谢谢,将查看参考文章。我们正在尝试让您提到的首选解决方案起作用,但尚未成功。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 2022-06-29
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多