Node.js的运行和放大器;为的WebAPI同一站点与IIS同一台服务器上 [英] Run node.js & WebAPI on same server for same site with IIS

查看:105
本文介绍了Node.js的运行和放大器;为的WebAPI同一站点与IIS同一台服务器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待慢慢转换 Node.js的应用程序 ASP.NET的WebAPI 2.0 。我目前使用 IIS ,将与 IIS 坚持下去。所以,我想收留他们在同一台服务器上,但一些直接的URI到新的平台。

I'm looking to slowly convert a Node.js application over to ASP.NET WebAPI 2.0. I'm currently using IIS and will stick with IIS. So, I would like to host them on the same server but direct some URIs over to the new platform.

我将如何做到这一点的的web.config ?当前的web.config 的node.js 看起来像这样:

How would I do this in the web.config? The current web.config for node.js looks like so:

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

我在想,我应该写一个新的规则,它列出了URI来转到的WebAPI 。但是,我不太清楚该怎么做。我的猜测是,我想补充一个条件为每个 URI 输入属性。我也在想,我应该指向 ASP.NET的WebAPI 项目,但我更无能我应该如何去这样做,因为的Node.js 我只是在 app.js 文件指向。

I was thinking that I should be writing a new rule which lists the URIs to go to the WebAPI. But, I'm not quite sure how to do that. My guess is that I would add a condition for each URI with the input attribute. I was also thinking I should point to the ASP.NET WebAPI project but I am even more clueless how I should go about doing that since Node.js I'm just pointing at the app.js file.

推荐答案

好吧,这是我落得这样做。它实际上是pretty直线前进。但是,当你不熟悉IIS它可以是艰巨的。

OK, this is what I ended up doing. It was actually pretty straight forward. But when you are not familiar with IIS it can be daunting.

我把原来的的web.config 节点目录。我觉得 iisnode 处理与的WebAPI 干扰的配置,如果你不知道。因此,新的的node.js 的web.config 节点目录应该是这样的:

I put the original web.config in with the node directory. I think the iisnode handler interferes with WebAPI config if you don't. So, the new node.js web.config in the node directory would look 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="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 。这意味着我将不得不编写一些自定义code来处理 gzip压缩文件重写 - 我以后会明白这一点。我也添加到每个重写 规则属性 stopProcessing 。这也被搞乱了code,因为它不会真正改写,我想它也一样,因为重写将被覆盖。请注意,接受版本头实际上并没有尚未测试过 - 我没有任何理由相信它不会,虽然工作。最后一个重写点的所有 URI S到的WebAPI 应用在默认情况下。

For root web.config I made it point to static files directly, bypassing node.js. Which means I'm going to have to write some custom code to handle rewrites for gzipped files - I'll figure that out later. I also added the attribute stopProcessing to each rewrite rule. This was also messing up the code, as it wouldn't actually rewrite where I wanted it too, since the rewrite would be overwritten. Note that the accept versioning header hasn't actually been tested yet - I don't have any reason to believe it wouldn't work though. The last rewrite points all uris to the webapi app by default.

的WebAPI 的项目,我不得不路线我所有的路线的WebAPI / API ,因为它不是在根文件夹。之后,我从迁移的node.js 一切我可能会做出的WebAPI 目录的根目录的项目,所以它赢得'T需要的WebAPI 在我的路由了。不过这一切从客户端隐藏。

In the WebAPI project I had to route all my routes to webapi/api since it isn't in the root folder. After I migrate everything from node.js I will probably make the webapi directory the root folder for the project so it won't need the webapi in my routing anymore. But this is all hidden from the client.

因此​​,这里的实际code:

So here's the actual code:

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

这篇关于Node.js的运行和放大器;为的WebAPI同一站点与IIS同一台服务器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆