跳到主要内容
CNode

nodejs 页面并发访问 如何防止阻塞

社区
Ppetersun发布于 13 年前最后回复 13 年前
256000

刚刚开始学习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)

Z
zaobao#113 年前

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

参与回复

登录后即可参与回复。登录