若希望在 Windows Server 的 IIS 上運行 Express,iisnode 可把 Node.js 程式托管進 IIS,並沿用 IIS 的進程管理、壓縮、日誌與權限控制等功能。
需準備 Node.js、IIS 的 URL Rewrite 模組,以及 iisnode。建立站點後,可用 web.config 把請求導向程式入口。
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="bin/www" verb="*" modules="iisnode" resourceType="Unspecified" requireAccess="Script" />
</handlers>
<rewrite>
<rules>
<rule name="all">
<match url="/*" />
<action type="Rewrite" url="bin/www" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>若出現 hiddenSegment 錯誤,是因為 ASP.NET 會把 bin 視為特殊目錄。可暫時把目錄改名,例如 launch/www,並同步調整 web.config。
iisnode 可能導致 req.ip 取不到值。可保留 server variable:
<iisnode promoteServerVars="REMOTE_ADDR" />
Express 中可重新定義 getter:
app.use(function(req, res, next) {
Object.defineProperty(req, 'ip', {
get: function() { return this.headers['x-iisnode-REMOTE_ADDR']; }
});
next();
});若上傳目錄跑到 launch 下,可在根目錄建立 launch.js 並 require('./bin/www'),再將 handler 指向 launch.js。iisnode 當時仍有不成熟之處,實際部署時需依版本調整。