一、安裝node.js的x86版本:
這樣,node.js會安裝在C:\Program Files (x86)\nodejs,這符合iisnode express7版本的期待。
二、安裝iisnode express的x86版本
在以下鏈接:https://github.com/tjanczuk/iisnode/wiki/iisnode-releases,選擇iisnode for iis 7 express (webmatrix) 。注意這明顯是x86版本,所以默認情況下,node.js也應安裝x86版本。目前尚未提供編譯好的express的x64版本,需要手工下載iisnode的源碼編譯。以開發而論,我們使用x86版本即可。安裝了iisnode之后,默認在C:\Users\Administrator\Documents\IISExpress\config目錄下的applicationhost.config會修改,不需要我們手工修改。
三、在vs2012中創建網站,運行簡單的例子:
我們可以創建一個空白的網站,默認只包括一個web.config文件和一個server.js文件。
我們需要在ide的網站菜單中,選擇"使用iis express"
1、Web.Config中,我們指定iisnode模塊處理server.js:
<configuration>
<system.webServer>
<handlers>
<!-- indicates that the server.js file is a node.js application to be handled by the iisnode module -->
<add name="iisnode" path="server.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<!-- Don't interfere with requests for logs -->
<rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^[a-zA-Z0-9_\-]+\.js\.logs\/\d+\.txt$" />
</rule>
<!-- Don't interfere with requests for node-inspector debugging -->
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^server.js\/debug[\/]?" />
</rule>
<!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}" />
</rule>
<!-- All other URLs are mapped to the Node.js application entry point -->
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True" />
</conditions>
<action type="Rewrite" url="server.js" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
2、創建server.js文件,包括:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('Hello, world!');
}).listen(process.env.PORT || 8080);
3、在vs2012中運行:
可看到輸出的hellow world
4、若需要顯示默認頁:
可增加一個index.html文件,在server.js里面載入。
四、為vs2012安裝typescript模版:
1、下載地址:
http://www.microsoft.com/en-us/download/details.aspx?id=34790
我們選擇0.82版本
2、安裝:需要重啟
3、重啟后我們創建一個typescript項目
4、將上述web.config和server.js加入,可正常的啟動。
五、node.js的網站開發:
最簡易的方式,顯然是靜態網頁加上node.js后端。但由於我們多數時候,需要在服務端創建動態的網頁,那么需要一種模版機制,如此可將數據和網頁融合起來。所以我們需要express,我們選擇ejs模版。