将多个 node.js 应用程序部署到一个 Web 应用程序中失败

时间:2021-02-18 12:58:24

标签: node.js azure azure-web-app-service azure-webapps

我了解到可以在一个 Azure Web 应用程序中部署多个 node.js 应用程序。所以我想尝试一下。我使用 express 创建了三个 node.js 应用程序,并将它们放置如下:

TestApplication
    -testapp0
        node_modules
        index.js
        package.json
        package-lock.json
        web.config
    -testapp1
        node_modules
        index.js
        package.json
        package-lock.json
        web.config
    -testapp2
        node_modules
        index.js
        package.json
        package-lock.json
        web.config

每个 index.js 都有几乎完全相同的代码。输出是唯一改变的东西。 在 web.config 中,我更改了处理程序和规则的名称。

index.js

const express = require("express");
const app = express();


app.get("/", (_, res)=>{
    res.status(200).send("Welcome to TestApp0"); // testapp1: Welcome to TestApp1, testapp2: Welcome to TestApp2 
});
app.get("/test", (_, res)=>{
    res.status(200).send("TestApp0 TestPath"); //testapp1: TestApp1 TestPath, testapp2: TestApp2 TestPath
});

const PORT = process.env.PORT || 1337;
app.listen(PORT, () => console.log(`Listening to http://localhost:${PORT}`));

web.config

<?xml version="1.0" encoding="utf-8"?>

<configuration>
  <system.webServer>
    <webSocket enabled="false" />
    <handlers>
      <!-- name="iisnode1" for testapp1 and name="iisnode2" for testapp -->
      <add name="iisnode0" path="index.js" verb="*" modules="iisnode"/>
    </handlers>
    <rewrite>
      <rules>
        <!-- name="NodeInspector1" for testapp1 and name="NodeInspector2" for testapp -->
        <rule name="NodeInspector0" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^index.js\/debug[\/]?" />
        </rule>
        <!-- name="StaticContent1" for testapp1 and name="StaticContent2" for testapp -->
        <rule name="StaticContent0">
          <action type="Rewrite" url="public{REQUEST_URI}"/>
        </rule>
        <!-- name="DynamicContent1" for testapp1 and name="DynamicContent2" for testapp -->
        <rule name="DynamicContent0">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
          </conditions>
          <action type="Rewrite" url="index.js"/>
        </rule>
      </rules>

    </rewrite>
    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin"/>
        </hiddenSegments>
      </requestFiltering>
    </security>
    <httpErrors existingResponse="PassThrough" />
  </system.webServer>
</configuration>

准备好代码后,我使用 VSCode 中的 Azure 应用服务扩展将 File TestApplication 部署到 Azure。

在此之前,我小心地更改了“路径映射”的设置:

| virtueller Pfad | Physischer Pfad       |     Typ     |
---------------------------------------------------------
|       /         | site\wwwroot\testapp0 | Application |
|  /testapp1      | site\wwwroot\testapp1 | Application |
|  /testapp2      | site\wwwroot\testapp2 | Application |

当我启动应用程序时,我会根据需要直接进入 testapp0,当我输入“/test”路径时,它会显示我想要的输出“TestApp0 TestPath”。

我想在屏幕上看到以下内容:

"Welcome to TestApp0" when I go to "/"              it works
"TestApp0 TestPath"   when I go to "/test"          it works
"Welcome to TestApp1" when I go to "/testapp1"      instead I get "Cannot GET /testapp1"
"TestApp1 TestPath"   when I go to "/testapp1/test" instead I get "Cannot GET /testapp1/test"
"Welcome to TestApp2" when I go to "/testapp2"      instead I get "Cannot GET /testapp2"
"TestApp2 TestPath"   when I go to "/testapp2/test" instead I get "Cannot GET /testapp2/test"

我试图到处寻找实现这一目标的方法。但遗憾的是,我还没有找到有效的解决方案,而且我可以阅读的文献也不多。

我认为这不起作用,因为节点应用程序 (testapp0) 查找名为“/testapp1”的路径,但没有任何路径。这就是为什么我收到“无法获取”错误的原因。但是我想在输入“/testapp1”后立即切换到另一个节点应用程序(testapp1),然后使用“/testapp1/test”获得所需的输出。我究竟做错了什么?我没有发现任何有用的东西,而且我是 Azure 的新手。因此,我们不胜感激指导。

感谢您的宝贵时间!

0 个答案:

没有答案