首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何处理Node.js中的多个并行HTTP请求?

如何处理Node.js中的多个并行HTTP请求?

提问于 2018-03-21 23:50:10
回答 2关注 0查看 261

我知道Node是非阻塞的,但默认http.listen(8000)意味着所有的HTTP请求都是一次处理的。我想知道如何编写我的代码,以便我可以处理多个并行的HTTP请求。

为了说明问题,运行下面的代码,并同时在两个浏览器选项卡中加载。

var http = require('http');
http.createServer(function (req, res) {
    res.setHeader('Content-Type', 'text/html; charset=utf-8');
    res.write("<p>" + new Date().toString() + ": starting response");
    setTimeout(function () {
        res.write("<p>" + new Date().toString() + ": completing response and closing connection</p>");
        res.end();
    }, 4000);
}).listen(8080);
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档