[英]Run node.js & WebAPI on same server for same site with IIS
我想慢慢將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。 但是,我不太清楚該怎么做。 我的猜測是我會為每個帶有input
屬性的URI
添加一個條件。 我也在想我應該指向ASP.NET WebAPI
項目,但是我更加無能為力,因為Node.js
我只是指向app.js
文件。
好的,這就是我最終要做的事情。 實際上非常直接。 但是當你不熟悉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
文件的重寫 - 我稍后會詳細說明。 我還為每個rewrite
rule
添加了屬性stopProcessing
。 這也搞亂了代碼,因為它實際上不會重寫我想要的地方,因為重寫會被覆蓋。 請注意, accept
版本控制標頭尚未實際測試過 - 我沒有任何理由相信它不會起作用。 默認情況下,最后一次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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.