我想通过http模块获取TTFB。唯一的方法(IMO)是将侦听器添加到套接字数据事件中。但它似乎没有用。
这是一个代码示例。
var http = require('http');
var request = http.request('http://nodejs.org/');
request.end();
request.on('socket', function(socket) {
socket.once('data', function TTFB() {
console.log('Never happen');
});
});
request.on('response', function(response) {
console.log('status code', response.statusCode);
response.on('data', function noop(){});
response.on('end', function() {
console.log('Bytes read', request.socket.bytesRead);
});
});
答案 0 :(得分:0)
套接字没有数据事件。试着听一下连接。
request.on('socket', function(socket) {
socket.once('connect', function TTFB() {
//Socket has been assigned to a request.
});
});
您可以通过检查套接字对象来查看套接字发出的事件。
_events: {
end: {
[Function: g] listener: [Function: onend]
},
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
free: [Function],
close: [
[Function],
[Function: socketCloseListener]
],
agentRemove: [Function],
drain: [Function: ondrain],
error: [Function: socketErrorListener],
connect: {
[Function: g] listener: [Function]
}
}