Node.js+Express on IIS


就是想讓node.js的程序跑在iis的意思,參考這篇文章《Hosting express node.js applications in IIS using iisnode》即可。

開始參考的是這篇文章《 Installing and Running node.js applications within IIS on Windows - Are you mad?》,其實也講得很清楚了,只是因為太長,中間講到

WCAT (WEB CAPACITY ANALYSIS TOOL)去了,我還以為講完了~~結果把網站一發布,跑不起來,發現需要手動編寫web.config來加入handler,OK,原生的node程序跑起來了。結果express下的網站又沒跑起來,最終解決后,串起來:

  1. 下載安裝node,需要注意的是,下的64位版的node是裝到64位的program files路徑下的,你需要下32位版的,或者直接下一個node.exe到32位program files目錄下,新建一個nodejs文件夾放進去;
  2. 下載安裝iisnode,此時如此你的c:\program files(x86)\nodejs\node.exe不存在的話它就會報錯退出了,這就是我第一步如此提示的原因,裝完后管理員權限執行安裝目錄下的setupsamples.bat可立即體驗下,沒必要;
  3. 下載安裝url rewrite包,express需要
  4. 將你的node網站發布到iis,比如:http://localhost:8081,應用程序池的.net版本任選,無意義

發布網站后做兩件事:

1,將監聽的端口改為:process.env.PORT,如在express下:app.set('port',process.env.PORT||3000);這樣即能保留iis端口又能讓node伺服網站的時候使用自定義的(如3000)端口;

2,在網站根目錄下添加web.config文件,加入如下內容:

<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="app.js" verb="*" modules="iisnode" />
   </handlers>
 
    <!-- use URL rewriting to redirect the entire branch of the URL namespace
    to app.js node.js application; for example, the following URLs will 
    all be handled by app.js:
    
        http://localhost/foo
        http://localhost/bar
        
    -->
 
    <rewrite>
      <rules>
        <rule name="main">
          <match url="/*" />
          <action type="Rewrite" url="app.js" />
        </rule>
      </rules>
    </rewrite>
 
    <!-- exclude node_modules directory and subdirectories from serving
    by IIS since these are implementation details of node.js applications -->
    
    <security>
      <requestFiltering>
        <hiddenSegments>
          <add segment="node_modules" />
        </hiddenSegments>
      </requestFiltering>
    </security>    
    
  </system.webServer>
</configuration>

這樣,所有/開頭的url都由app.js接管,app.js又被注冊成了一個http handler,使用iisnode模塊,這時候一個標准的express就可以正常跑起來了

 

最后,我還用了angular,正好試一下上述url rewrite會不會把angular的url路由也接管掉,實測不會,如果用angular或其它一些框架來做SPA(單頁應用)程序是沒問題的,原因應該是這些框架都是用切換hash的方式來路由,github那樣完全更改url的來不及測試,有時間加上測試結果

 

P.S. 上面介紹安裝配置過程的時候,講到安裝完iisnode后可立即體驗一下的那個bat程序可以不干,其實還是建議執行一下的,它會在默認網站下生成一個node虛擬路徑,里面有比較豐富的示例和說明,特別是上述web.config文件,里面還有很多可配置的地方,在configuration/readme.htm文件里面有詳細的描述,建議執行和詳閱。

P.S.P.S 某次重裝電腦(win8.1)后我又對着自己的文檔來干這事,結果卻碰到了http error 5500.19,錯誤代碼:0x80070021,描述:This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault="Deny"), or set explicitly by a location tag with overrideMode="Deny" or the legacy allowOverride="false".結果發現這不是問題,我自己的iis沒配置好罷了,從添加刪除功能里面,把CGI勾上即可:http://stackoverflow.com/questions/9794985/iis-this-configuration-section-cannot-be-used-at-this-path-configuration-lock


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM