【发布时间】:2019-07-13 16:43:44
【问题描述】:
我正在运行仅使用 HTTP 模块的准系统 Nodejs 服务器。我已经创建了一个 HTTP 服务器,并且正在监听套接字连接和请求。我注意到,当我使用 chrome 并转到 localhost 时,连接了三个套接字,并且向“/”发出了两个请求。我知道,使用其他一些网络服务器,如果 Chrome 没有收到快速响应(大约 5 秒),我已经看到多次请求相同的内容,但我立即发送响应并且 Chrome 仍在连接/请求多个次。
这是预期的吗?如果是,我应该处理重复的请求吗?
我的相关代码
let server = http.createServer();
server.listen({
host: host,
port: port
});
server.on('connection', function(socket){
// gets printed 3 times
console.log('connection')
});
server.on('request', function(request, response){
// gets printed two times
console.log('hi')
// yet chrome only receives one response (seemingly)
response.end('hi')
});
编辑:解决了一半。现在我正在打印request.url,我看到了
/
和
favicon.ico
所以有 2 个请求,但仍然有 3 个套接字连接。我猜每个请求都在一个新的套接字上?
【问题讨论】:
标签: javascript node.js