2012-03-30 160 views
0

我正在關注一個Node.js tutorial,並在「阻塞和非阻塞」一節中有它應該說明阻塞問題的代碼。爲什麼Node.js中沒有這個代碼塊?

function start() { 
     console.log("Request handler 'start' was called."); 

     function sleep(milliSeconds) { 
      var startTime = new Date().getTime(); 
      while (new Date().getTime() < startTime + milliSeconds); 
     } 

     sleep(10000); 

     return "Hello Start"; 
} 

function upload() { 
     console.log("Request handler 'upload' was called."); 
     return "Hello Upload"; 
} 

exports.start = start; 
exports.upload = upload; 

index.js

var server = require("./server"); 
var router = require("./router"); 
var requestHandlers = require("./requestHandlers"); 

var handle = {} 
handle["/"] = requestHandlers.start; 
handle["/start"] = requestHandlers.start; 
handle["/upload"] = requestHandlers.upload; 

server.start(router.route, handle); 

我試圖加載http://localhost:8888/starthttp://localhost:8888/upload。由於阻塞,它們都需要10秒才能加載,但它們都會立即加載。爲什麼?如果我直接在node.js中運行sleep()函數,它會阻塞,但不會在Web瀏覽器中運行。這不再是因爲某種原因需要處理的問題嗎?

+0

您鏈接到本地​​計算機上的文件。你最好發佈代碼本身。 – Dennis 2012-03-30 21:16:05

+0

我修正了鏈接幷包含了代碼。 – neuromancer 2012-03-30 21:29:26

+0

你的代碼應該工作,你如何調用'start'函數? – stewe 2012-03-30 21:46:29

回答

1

您的代碼看起來不錯,您的瀏覽器可能會緩存響應。嘗試將?random = 1234添加到URL中,看看是否會花費更長時間。

+0

我試過了,我甚至清除了緩存,但沒有幫助。現在它按預期工作,但我不知道爲什麼。 – neuromancer 2012-03-30 23:05:55

相關問題