Run node.js & WebAPI on the same server for the same site with IIS

I want to slowly convert the application Node.jsto ASP.NET WebAPI 2.0. Currently I use IISand will stick IIS. Therefore, I would like to host them on the same server, but forward some URIs to the new platform.

How do I do this in web.config? Current web.configfor Node.jslooks like this:

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

File structure:

- web.config (the one shown above)
  -> node
      - app.js
      - ...
  -> webapi
      - web.config
      - global.asax
      - ...

, , URI, WebAPI. , . , URI input. , ASP.NET WebAPI, , , Node.js app.js.

+4
1

, , . . IIS, .

web.config node. , iisnode WebAPI, . , node.js web.config node :

<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 versioning - , . rewrite uri 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>
0

Source: https://habr.com/ru/post/1626928/


All Articles