前言:最近因為項目原因,需要在IIS下部署node項目,在此之前,曾經部署過類似的項目,因此在這次部署還算比較順利,只是在其中遇到了幾個比較坑的問題,所以這次使用博客記錄下來,如有園友遇到過類似問題,希望對你有所幫助。
一、前期准備
1、node.js(下載地址:https://nodejs.org/en/),根據自己的需要安裝對應版本
2、iisnode(下載地址:https://github.com/tjanczuk/iisnode)
3、IIS的URL Rewrite模塊(下載地址:https://www.iis.net/downloads/microsoft/url-rewrite)
依次安裝好以上軟件,記錄下node.js的安裝路徑(例如我的安裝路徑是:C:\software\nodejs),在后續中會用到。
如果需要測試iisnode是否安裝成功,可以用
%programfiles%\iisnode\setupsamples.bat
執行后:
來安裝iisnode自帶的一個例子,安裝完成后,訪問:http://localhost/node,如果網頁不能編譯出現以下錯誤的解決辦法:
The iisnode module is unable to start the node.exe process. Make sure the node.exe executable is available at the location specified in the system.webServer/iisnode/@nodeProcessCommandLine element of web.config. By default node.exe is expected in one of the directories listed in the PATH environment variable
在要打開在頁面所在文件夾下的web.config中添加以下內容:
nodeProcessCommandLine=""%programfiles%\nodejs\node.exe"" interceptor=""%programfiles%\iisnode\interceptor.js"" />,注意配置文件里只允許有一個 iisnode 屬性設置
如圖
二、部署項目
在IIS下新建一個站點(此過程不在贅述),然后在項目下打開控制台,安裝項目需要依賴的包,執行:
npm install
安裝完成后會在項目中新增一個文件夾:
然后編輯web.config:
<configuration> <system.webServer> <!-- bin/www 是Express示例默認的啟動程序 --> <handlers> <add name="iisnode" path="bin/www" verb="*" modules="iisnode" /> </handlers> <rewrite> <rules> <rule name="myapp"> <match url="/*" /> <action type="Rewrite" url="bin/www" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
此時在瀏覽器中打開地址http://localhost/myapp/,將出現404錯誤,原因是bin目錄是默認輸出目錄,默認不允許模塊調用
HTTP 錯誤 404.8 - Not Found
請求篩選模塊被配置為拒絕包含 hiddenSegment 節的 URL 中的路徑。
解決辦法:
在你的項目根目錄下,新建一個index.js的文件,然后將bin/www里面的代碼剪切過來,同時可以刪除bin文件夾了,並且再次修改你的web.config文件,將入口程序改成index.js:
<configuration> <system.webServer> <!-- indicates that the hello.js file is a node.js application to be handled by the iisnode module --> <handlers> <add name="iisnode" path="index.js" verb="*" modules="iisnode" resourceType="Unspecified" requireAccess="Script" preCondition="bitness64" /> </handlers> <rewrite> <rules> <rule name="all"> <match url="/*" /> <action type="Rewrite" url="index.js" /> </rule> </rules> </rewrite> <iisnode node_env="%node_env%" nodeProcessCountPerApplication="1" maxConcurrentRequestsPerProcess="1024" maxNamedPipeConnectionRetry="100" namedPipeConnectionRetryDelay="250" maxNamedPipeConnectionPoolSize="512" maxNamedPipePooledConnectionAge="30000" asyncCompletionThreadCount="0" initialRequestBufferSize="4096" maxRequestBufferSize="65536" watchedFiles="*.js;node_modules\*;routes\*.js;views\*.jade" uncFileChangesPollingInterval="5000" gracefulShutdownTimeout="60000" loggingEnabled="true" logDirectory="iisnode" debuggingEnabled="true" debugHeaderEnabled="false" debuggerPortRange="5058-6058" debuggerPathSegment="debug" maxLogFileSizeInKB="128" maxTotalLogFileSizeInKB="1024" maxLogFiles="20" devErrorsEnabled="true" flushResponse="false" enableXFF="false" configOverrides="iisnode.yml" nodeProcessCommandLine="C:\software\nodejs\node.exe" promoteServerVars="REMOTE_ADDR" /> <defaultDocument> <files> <add value="index.js" /> </files> </defaultDocument> <!-- One more setting that can be modified is the path to the node.exe executable and the interceptor: <iisnode nodeProcessCommandLine=""%programfiles%\nodejs\node.exe"" interceptor=""%programfiles%\iisnode\interceptor.js"" /> --> </system.webServer> </configuration>
此時你就可以運行你的項目了,如果在運行的時候輸出如下錯誤:
iisnode encountered an error when processing the request.
請檢查你的index.js文件的require路徑是否正確:
var app = require('./app');
var debug = require('debug')('myapp:server');
var http = require('http');
正常情況下,此時你的項目就可以正常運行了,但是!!!還沒完!!,最坑人的地方來了,你的項目可能會一直報一下錯誤:
iisnode encountered an error when processing the request.
HRESULT: 0x2
HTTP status: 500
HTTP reason: Internal Server Error
You are receiving this HTTP 200 response because system.webServer/iisnode/@devErrorsEnabled configuration setting is 'true'.
In addition to the log of stdout and stderr of the node.exe process, consider using debugging and ETW traces to further diagnose the problem.
The node.exe process has not written any information to stderr or iisnode was unable to capture this information. Frequent reason is that the iisnode module is unable to create a log file to capture stdout and stderr output from node.exe. Please check that the identity of the IIS application pool running the node.js application has read and write access permissions to the directory on the server where the node.js application is located. Alternatively you can disable logging by setting system.webServer/iisnode/@loggingEnabled element of web.config to 'false'.
找了很久錯誤,始終以為是哪個環節安裝好,或者依賴包沒有安裝正確,始終沒有解決,在快絕望的時候,終於在stackoverflow上找到了一位外國網友記錄這個錯誤解決辦法:
地址:https://stackoverflow.com/questions/24028537/iisnode-encountered-an-error-when-processing-the-request/24038377
老鐵們!看到這個解決辦法我也是想哭了,還有這個??於是修改了項目的user權限,問題就迎刃而解了。
三、總結
永遠不要忽略細節!!!