【问题标题】:Run node.js & WebAPI on same server for same site with IIS使用 IIS 在同一台服务器上为同一站点运行 node.js 和 Web API
【发布时间】:2016-05-10 12:37:50
【问题描述】:

我希望将Node.js 应用程序慢慢转换为ASP.NET WebAPI 2.0。我目前正在使用IIS,并将坚持使用IIS。所以,我想将它们托管在同一台服务器上,但将一些 URI 指向新平台。

我将如何在web.config 中执行此操作? node.js 的当前 web.config 如下所示:

<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 文件。

【问题讨论】:

  • 我认为您的第二条和第三条规则标签中应该有match 标签。并且操作应该类似于&lt;action type="Rewrite" url="beta/app.js?action={R:1}"/&gt;。 {R:1} 是 url 中的匹配部分。由于我不知道您要应用什么规则,您可以提供一些示例。
  • 感谢@kiro 的指点。我不确定它是否适用于我正在做的事情,因为原始 node.js web.config 工作正常。您可以在下面的解决方案中看到我需要做的事情。在尝试创建单独的项目时,我做错了一些事情。

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


【解决方案1】:

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

我将原始的web.config 放入node 目录中。如果您不这样做,我认为iisnode 处理程序会干扰WebAPI 配置。因此,node 目录中的新 node.js web.config 将如下所示:

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

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

WebAPI 项目中,我必须将所有路由路由到webapi/api,因为它不在根文件夹中。在我从node.js 迁移所有内容后,我可能会将webapi 目录作为项目的根文件夹,因此它不再需要在我的路由中使用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>

【讨论】:

    猜你喜欢
    • 2012-07-26
    • 2023-04-03
    • 2016-07-23
    • 2020-06-27
    • 2021-08-26
    • 2021-11-20
    • 2012-04-07
    • 2015-05-27
    • 1970-01-01
    相关资源
    最近更新 更多