nodejs 页面并发访问 如何防止阻塞
发布于 11 年前 作者 petersun 4854 次浏览 最后一次编辑是 8 年前

刚刚开始学习node,在看教程http://www.nodebeginner.org/index-zh-cn.html的过程中,遇到这个问题: function start(){ function onReq(req,res){ res.writeHead(200,{“Content-Type”:“text/plain”}); sleep(5000); res.write(“hello”); res.end(); } http.createServer(onReq).listen(9999); console.log(“server has started”); } function sleep(mileSecond){ var time=new Date().getTime(); while(new Date().getTime()<time+mileSecond){

} }

如果同时打开两个页面,访问http://localhost:9999/ 若使先启动的页面不会对第二个页面造成阻塞,代码应该怎么写? 大家帮忙指导一下,谢谢~

2 回复

你把sleep去掉,代码里都写明阻塞,还怎么不阻塞? 或者用cluster开两个进程,第一条进程还是阻塞的。

好,谢谢,现在思路清晰多了。我去看看cluster

回到顶部