Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

nodejs处理http请求是串行的吗

$
0
0

nodejs在处理http或者https请求的时候,是把和该请求有关的异步任务都执行完,才能处理下一个请求吗,这点挺困惑的,下面是我的代码实践。

在node里面模拟一个异步任务

const http = require('http');
const url = require('url');
http.createServer(function(req, res) {
	// 革除/favicon.ico额外请求
	if(url.parse(req.url).path == '/favicon.ico') {
		return;
	}
	// 下面才是实践的主要内容
	console.log("开始处理新请求了");
	// 模拟5秒的长时间的异步任务
	setTimeout(function() {
		console.log('执行完异步任务了');
		res.end('ok');
	}, 5000)
}).listen(8080)

前端模拟在极短时间内有三个请求先后访问node服务器

for(var i = 0; i < 3; i++) {
		window.open("http://localhost:8080/", "_blank");
}

下面是实际运行结果捕获.PNG

结果显示node在处理请求的时候会把和该请求有关的异步代码执行完毕,才会处理下一个请求,不知道我的理解正不正确,请大家赐教,谢谢。


Viewing all articles
Browse latest Browse all 14821

Latest Images

Trending Articles