Nodejs in Visual Studio Code 08.IIS

1.開始javascript

  本文部份內容均轉載自文章:php

http://www.hanselman.com/blog/InstallingAndRunningNodejsApplicationsWithinIISOnWindowsAreYouMad.aspxjava

https://github.com/tjanczuk/iisnodenode

  做者:SCOTT HANSELMANgit

  標題:Installing and Running node.js applications within IIS on Windows - Are you mad?github

  發佈日期:2011.08.28web

 

2.爲何我要在Windows IIS上運行Node.jsexpress

  首先,我主要編寫的是Asp.Net應用程序,對windows和iis很熟悉,若是在使用Node.js生產的應用若是能部署在IIS上,那麼我想很快就能將Node.js開發的應用發佈在生產環境中。windows

  雖然在IIS運行Node.js上並非最優選擇,可是我主要開發企業級應用,單機併發訪問量3K、4K足矣並不如大型互聯網企業那樣會遇到極大的併發、調優壓力。瀏覽器

  最後微軟有一幫人正在IIS上玩轉Node.js,我表示跟着入坑試一試。

  • 進程管理:iisnode模塊將會對node.exe進行簡單而暴力的全生命週期的進程管理來加強其可靠性。你沒必要擔憂什麼時候開啓、結束node.exe,或者開着軟件監控node.exe的運行狀態。
  • 多核服務器的可擴展性:node.exe是一個單進程運行的程序,你須要額外編寫基礎設施代碼來擴展其支持多核服務器,若是使用iisnode,你無須編寫代碼,將能夠配置打開多個進程的node.exe,同時將HTTP請求均衡的負載到多個node.exe上。
  • 自動更新:iisnode將會監視js文件,若是有任何更新將會自動回收並重啓node.exe加載新的js代碼,你沒必要擔憂正在執行的http請求,由於這些請求將會仍然使用舊版本的js直至執行完畢。
  • HTTP訪問日誌:iisnode將會把HTTP請求中調用console.log輸出的內存存儲在output中,這些輸出內容對於調試遠程服務器來講是很重要的資源。
  • 混合開發:iisnode是IIS的一個模塊而已,你在一個站點中能夠包含node.js、php、asp.net等多個應用程序。
  • 少許的代碼修改:爲了將node.js程序部署在IIS中你仍是須要修改一下node.js代碼的,好比process.env.PORT
  • 集成管理經驗:iisnode是IIS的一個模塊,你能夠用到IIS的一些特色以下
    • 端口共享,你能夠在80端口下部署多個不一樣的應用asp.net、node.js、php啊,愛誰誰。
    • HTTPS支持
    • Url rewriting
    • 壓縮
    • 緩存
    • 日誌

 

3.如何在IIS上運行Node.js

  • 環境要求
    • Windows Vista, Windows 7, Windows 8, Windows Server 2008, or Windows Server 2012
    • IIS 7.x with IIS Management Tools and ASP.NET
    • WebSocket functionality requires IIS 8.x on Windows 8 or Windows Server 2012
    • URL rewrite module for IIS
      • (Win10:https://www.microsoft.com/en-us/download/details.aspx?id=47337)
    • Latest node.js build for Windows
  • 爲IIS7.x/8.x安裝iisnode擴展
    • 爲 IIS 7.x/8.x 安裝 iisnode : x86 或 x64 - 取決於你的操做系統
    • 安裝示例, 以管理員權限打開CMD執行 %programfiles%\iisnode\setupsamples.bat
    • 瀏覽 http://localhost/node

 

4.學習iisnode

  經過上面安裝node示例,查看示例發現iis下新增了一個node目錄。

     這個node目錄運行在DefaultAppPool中(.Net 4.0集成)

 

  打開Express示例的文件夾,包括如下內容

  在Hello.js中定義了兩個不一樣的接口myapp/foo、myapp/bar

var express = require('express');

var app = express.createServer();

app.get('/node/express/myapp/foo', function (req, res) {
    res.send('Hello from foo! [express sample]');
});

app.get('/node/express/myapp/bar', function (req, res) {
    res.send('Hello from bar! [express sample]');
});

app.listen(process.env.PORT);

  在Web.Config中指定了,Hello.js做爲入口函數,同時將myapp/*請求映射給hello.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="hello.js" verb="*" modules="iisnode" />
    </handlers>

    <!-- use URL rewriting to redirect the entire branch of the URL namespace
    to hello.js node.js application; for example, the following URLs will 
    all be handled by hello.js:
    
        http://localhost/node/express/myapp/foo
        http://localhost/node/express/myapp/bar
        
    -->

    <rewrite>
      <rules>
        <rule name="myapp">
          <match url="myapp/*" />
          <action type="Rewrite" url="hello.js" />
        </rule>
      </rules>
    </rewrite>
    
  </system.webServer>
</configuration>

  打開示例頁http://localhost/node/express/readme.htm進一步查看調用狀況

  最後打開Chrome瀏覽器,輸入http://localhost/node/express/hello.js/debug,能夠遠程調試服務,真乃神器也。。

 

5.總結

  node.js + express便可以快速開發RestAPI和模版站點,又能夠在iis中進行部署,遠程調試,能夠知足項目須要。

  如下內容參考:Debugger Don't work https://github.com/tjanczuk/iisnode/issues/396

  因爲IIS7.X不支持WebSockets因此沒法使用最新的遠程調試器,可是能夠經過配置舊的遠程調試器(

<iisnode debuggerExtensionDll="iisnode-inspector.dll" />

)來支持IIS7.X,僅須要修改一下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="hello.js" verb="*" modules="iisnode" />
    </handlers>

    <!-- use URL rewriting to redirect the entire branch of the URL namespace
    to hello.js node.js application; for example, the following URLs will 
    all be handled by hello.js:
    
        http://localhost/node/express/myapp/foo
        http://localhost/node/express/myapp/bar
        
    -->

    <rewrite>
      <rules>
        <rule name="myapp">
          <match url="myapp/*" />
          <action type="Rewrite" url="hello.js" />
        </rule>
      </rules>
    </rewrite>
    <iisnode debuggerExtensionDll="iisnode-inspector.dll" />
  </system.webServer>
</configuration>

  舊調試器就是醜點。。

相關文章
相關標籤/搜索