簡體   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