我关注Node.js tutorial并参与"阻止和非阻止"它有这个代码可以证明阻塞问题。
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浏览器中阻止。这不再是出于某种原因必须处理的问题吗?
答案 0 :(得分:1)
您的代码看起来不错,您的浏览器可能会缓存响应。尝试将“random = 1234”添加到网址,看看是否需要更长时间。