我正在關注一個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/start和http://localhost:8888/upload。由於阻塞,它們都需要10秒才能加載,但它們都會立即加載。爲什麼?如果我直接在node.js中運行sleep()函數,它會阻塞,但不會在Web瀏覽器中運行。這不再是因爲某種原因需要處理的問題嗎?
您鏈接到本地計算機上的文件。你最好發佈代碼本身。 – Dennis 2012-03-30 21:16:05
我修正了鏈接幷包含了代碼。 – neuromancer 2012-03-30 21:29:26
你的代碼應該工作,你如何調用'start'函數? – stewe 2012-03-30 21:46:29