crossplatform---Nodejs in Visual Studio Code 10.IISNode

1.開始javascript

  Nodejs in Visual Studio Code 08.IIS : http://www.cnblogs.com/mengkzhaoyun/p/5410185.htmlcss

   參考此篇內容,部署一個iisnode示例html

2.將Node.js Express框架示例程序部署在IISNode中java

  •  建立一個Express示例程序
1
2
3
4
5
6
7
8
9
10
11
cd  D:\Libraries\Documents\Visual Studio Code
$ express myapp
   
create : myapp
create : myapp /package .json
......
   
install  dependencies:
cd  myapp && npm  install
run the app:
> SET DEBUG=myapp:* & npm start

  • 在IIS中建立一個myapp的目錄發佈此node程序
    • 應用程序池:DefaultAppPool(IIS默認的,.Net 集成)

  • 建立Web.config文件至myapp目錄
    • 注意上圖,Node Express的啓動程序是bin/www文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<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目錄是默認輸出目錄,默認不容許模塊調用
1
2
3
HTTP 錯誤 404.8 - Not Found
 
請求篩選模塊被配置爲拒絕包含 hiddenSegment 節的 URL 中的路徑。
  • myapp目錄下,新建一個index.js,嘗試解決此問題

  將bin/www代碼剪切過來,並刪除bin/www,修改Web.config中的入口程序爲index.jsnode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<configuration>
   <system.webServer>
     <!-- index.js 是myapp的啓動程序 -->
     <handlers>
       <add name= "iisnode"  path= "index.js"  verb= "*"  modules= "iisnode"  />
     </handlers>
     <rewrite>
       <rules>
         <rule name= "myapp" >
           <match url= "/*"  />
           <action type= "Rewrite"  url= "index.js"  />
         </rule>
       </rules>
     </rewrite>
   </system.webServer>
</configuration>
  • 此時從新瀏覽http://localhost/myapp/,出現運行時錯誤
    • 這個運行時錯誤表示node在執行index.js過程當中出錯,是個相對地址寫錯了的問題,node終於執行了,不容易啊,可能由於我將bin/www硬Copy過來忘記修正相對目錄了。。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
iisnode encountered an error when processing the request.
HRESULT: 0x2
HTTP status: 500
HTTP subStatus: 1002
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 last 64k of the output generated by the node.exe process to stderr is shown below:
Application has thrown an uncaught exception and is terminated:
Error: Cannot find module '../app'
     at Function.Module._resolveFilename (module.js:325:15)
     at Function.Module._load (module.js:276:25)
     at Module.require (module.js:353:17)
     at require (internal/module.js:12:17)
     at Object.< anonymous > (D:\Libraries\Documents\Visual Studio Code\myapp\index.js:7:11)
     at Module._compile (module.js:409:26)
     at Object.Module._extensions..js (module.js:416:10)
     at Module.load (module.js:343:32)
     at Function.Module._load (module.js:300:12)
     at Module.require (module.js:353:17)
  • 修改index.js
    • ../app改爲./app就少了個點,看出來了麼
1
2
3
var  app = require( './app' );
var  debug = require( 'debug' )( 'myapp:server' );
var  http = require( 'http' );
  • 再訪問,出現錯誤Cannot find module 'serve-favicon' ,粗心忘記NPM INSTALL了。。。
  • 執行CMD命令 npm install爲myapp安裝依賴包
  • 從新訪問網頁http://localhost/myapp/,出現404 Not Found
    • 虛擬路徑錯誤,須要在app.js中修改虛擬路徑
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Not Found
 
404
Error: Not Found
     at D:\Libraries\Documents\Visual Studio Code\myapp\app.js:30:13
     at Layer.handle [as handle_request] (D:\Libraries\Documents\Visual Studio Code\myapp\node_modules\express\lib\router\layer.js:95:5)
     at trim_prefix (D:\Libraries\Documents\Visual Studio Code\myapp\node_modules\express\lib\router\index.js:312:13)
     at D:\Libraries\Documents\Visual Studio Code\myapp\node_modules\express\lib\router\index.js:280:7
     at Function.process_params (D:\Libraries\Documents\Visual Studio Code\myapp\node_modules\express\lib\router\index.js:330:12)
     at next (D:\Libraries\Documents\Visual Studio Code\myapp\node_modules\express\lib\router\index.js:271:10)
     at D:\Libraries\Documents\Visual Studio Code\myapp\node_modules\express\lib\router\index.js:618:15
     at next (D:\Libraries\Documents\Visual Studio Code\myapp\node_modules\express\lib\router\index.js:256:14)
     at Function.handle (D:\Libraries\Documents\Visual Studio Code\myapp\node_modules\express\lib\router\index.js:176:3)
     at router (D:\Libraries\Documents\Visual Studio Code\myapp\node_modules\express\lib\router\index.js:46:12)
  • 修改app.js,注意發佈路徑是myapp,終於看到了Welcome to Express不容易啊,別忘了改public目錄的路徑
1
2
3
4
app.use( '/myapp' ,express. static (path.join(__dirname,  'public' )));
 
app.use( '/myapp' , routes);
app.use( '/myapp/users' , users);
  • 修改views/layout.jade,注意有個相對CSS相對路徑,去掉最前的斜槓
1
2
3
4
5
6
7
doctype html
html
   head
     title= title
     link(rel= 'stylesheet' , href= 'stylesheets/style.css' )
   body
     block content

  • 在Chrome中打開http://localhost/myapp/index.js/debug遠程調試,出現錯誤,仍是圖樣啊
    • 看來這個index.js/debug這個鏈接被app執行了...
    • 到目前爲止,全部錯誤徹底沒法調試,全靠我經(wo)驗(shi)豐(cai)富(de)

  • 出現上面錯誤的緣由是UrlRewriter配置

  將全部myapp下的執行請求都交給index.js去執行了,顯然myapp/index.js/debug這個地址不該該給index.js去執行。git

  修改web.config中的url rewriter節,將index.js/debug這個地址設置成不匹配negate="true"。github

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
< configuration >
   < system.webServer >
     <!-- index.js 是myapp的啓動程序 -->
     < handlers >
       < add  name="iisnode" path="index.js" verb="*" modules="iisnode" />
     </ handlers >
     < rewrite >
       < rules >
         < rule  name="myapp">
           < match  url="index.js/debug" negate="true" />
           < action  type="Rewrite" url="index.js" />
         </ rule >
       </ rules >
     </ rewrite >
   </ system.webServer >
</ configuration >

  • 終於見到Node Inspector了,在瀏覽器中輸入http://localhost/myapp/users,Node Inspector將捕獲這個處理

 

3.總結web

  iisnode各類叼,支持發佈後跨平臺遠程調試,若是發佈過程當中遇到問題怎麼辦呢,iisnode發佈5年了(目前是0.2.*版本),應該積累了很多issues了,一篇篇翻看,或者去問做者解決吧。express

     示例代碼,請前往:https://github.com/Mengkzhaoyun/nodepractise npm

     04.iisNode&myApp

http://www.cnblogs.com/mengkzhaoyun/p/5414501.html

相關文章
相關標籤/搜索