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,我表示跟着入坑試一試。
3.如何在IIS上運行Node.js
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>
舊調試器就是醜點。。