iis - 使用 IIS 在同一台服务器上运行 node.js 和 Web API

标签 iis asp.net-web-api iis-7

我希望慢慢转换 Node.js申请转至 ASP.NET WebAPI 2.0 .我目前正在使用 IIS并且会坚持使用 IIS .所以,我想将它们托管在同一台服务器上,但将一些 URI 定向到新平台。

我将如何在 web.config 中执行此操作?当前web.confignode.js看起来像这样:

<configuration>
  <system.webServer>

    <handlers>
      <!-- indicates that the app.js file is a node.js application
           to be handled by the iisnode module -->
      <add name="iisnode" path="beta/app.js" verb="*" modules="iisnode" />
    </handlers>

    <rewrite>
      <rules>
        <!-- Don't interfere with requests for node-inspector debugging -->
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^beta/app.js\/debug[\/]?" />
        </rule>

        <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->

        <rule name="StaticContent">
          <action type="Rewrite" url="beta/public{REQUEST_URI}" />
        </rule>

        <!-- All other URLs are mapped to the Node.js application entry point -->
        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True" />
          </conditions>
          <action type="Rewrite" url="beta/app.js" />
        </rule>

      </rules>
    </rewrite>
    <httpErrors errorMode="Detailed"/>
  </system.webServer>
</configuration>

文件结构为:
- web.config (the one shown above)
  -> node
      - app.js
      - ...
  -> webapi
      - web.config
      - global.asax
      - ...

我在想我应该写一个新规则,列出要转到 WebAPI 的 URI。 .但是,我不太确定如何做到这一点。我的猜测是我会为每个 URI 添加一个条件与 input属性。我还想我应该指向 ASP.NET WebAPI项目,但自 Node.js 起我更不知道我应该如何去做这件事我只是指着 app.js文件。

最佳答案

好的,这就是我最终要做的。它实际上非常简单。但是当您不熟悉 IIS 时,它可能会令人生畏。

我把原来的web.config在与 node目录。我认为iisnode处理程序干扰 WebAPI如果没有,请配置。所以,新node.js web.confignode目录如下所示:

<configuration>
  <system.webServer>

    <handlers>
      <!-- indicates that the app.js file is a node.js application
           to be handled by the iisnode module -->
      <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
    </handlers>

    <rewrite>
      <rules>

        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
            <match url="^app.js\/debug[\/]?" />
        </rule>

      </rules>
    </rewrite>
    <httpErrors errorMode="Detailed"/>

  </system.webServer>
</configuration>

对于根 web.config我直接指向静态文件,绕过 node.js .这意味着我将不得不编写一些自定义代码来处理 gzipped 的重写。文件 - 稍后我会弄清楚。我还添加了属性 stopProcessing每个rewrite rule .这也弄乱了代码,因为它实际上也不会在我想要的地方重写,因为重写会被覆盖。请注意 accept版本控制头实际上还没有经过测试 - 我没有任何理由相信它不会起作用。最后 rewrite积分全uri s 到 webapi应用程序默认。

WebAPI项目我必须将所有路线路由到 webapi/api因为它不在根文件夹中。在我从 node.js 迁移所有内容之后我可能会制作 webapi directory 项目的根文件夹,因此它不需要 webapi在我的路由中了。但这对客户来说都是隐藏的。

所以这是实际的代码:
<configuration>
  <system.webServer>

    <rewrite>
      <rules>

        <!-- test item for webapi folder -->
        <rule name="StaticContent2" stopProcessing="true" >
            <conditions>
                <add input="{REQUEST_URI}" pattern="^/def" />
            </conditions>
            <action type="Rewrite" url="webapi{REQUEST_URI}" />
        </rule>

        <!-- rewrite static items which exist on node -->
        <rule name="Node Static" stopProcessing="true" >
            <conditions>
                <add input="{REQUEST_URI}" pattern=".*\.[A-Za-z2]{2,5}$" />
            </conditions>
            <action type="Rewrite" url="node/public{REQUEST_URI}" />
        </rule>

        <rule name="WebAPI Version 2" stopProcessing="true">
            <conditions>
                <add
                    input="{HEADER_ACCEPT}"
                    pattern="vnd.fieldops.v2"
                    ignoreCase="true"
                />
            </conditions>
            <action type="Rewrite" url="webapi{REQUEST_URI}" />
        </rule>

        <!-- rewrite to node for dynamic items -->
        <rule name="Node Dynamic" stopProcessing="true" >
            <conditions>
                <add
                    input="{REQUEST_URI}"
                    pattern="^/api/(dealerservicereports|chat|dealers|dealerequipment|dealercloseout|publications|tokens|users|\?)"
                    ignoreCase="true"
                />
            </conditions>
            <action type="Rewrite" url="node/app.js" />
        </rule>

        <!-- rewrite everything else to webapi -->
        <rule name="WebAPI Dynamic" stopProcessing="true" >
            <action type="Rewrite" url="webapi{REQUEST_URI}" />
        </rule>

      </rules>
    </rewrite>
    <httpErrors errorMode="Detailed"/>

  </system.webServer>
</configuration>

关于iis - 使用 IIS 在同一台服务器上运行 node.js 和 Web API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35141071/

相关文章:

git - 致命 : Invalid ID Jenkins IIS

c# - IIS 8.0 HTTP 错误 404.17 - 未找到请求的内容似乎是脚本,不会由静态文件处理程序提供服务

asp.net - Azure应用程序服务-如何使Always On适用于特定应用程序?

http-post - 如何从 ASP.NET Web API(POST 或 PUT)调用获取 httppostedfile?

ASP.NET 处理程序未在 IIS7 上运行

windows - 在 Windows/NTFS 上,可以将符号链接(symbolic link)移动到另一台计算机吗?

c# - DLLImport 找不到 dll

reactjs - Identity Server 4 SPA 社交登录

iis - WebSocket 托管不在端口 80 防火墙问题

asp.net - Windows 身份验证当前用户模拟 401 未经授权