我正在使用nodejs
创建一个网站。在我的网站页面中,我要求Google然后解析其结果并将其发送给用户。我使用npm
的{{3}}模块来向Google请求。 request
模块有一个名为time
的标志,如果我将其设置为true,那么request
会为我提供请求时间的详细信息。我这样要求Google:
var express = require('express');
var router = express.Router();
var request = require('request');
router.get('/', function(req, res, next) {
return request({
url: 'https://<google-ip>/search?q=' + req.query.query + '&num=30&start=0',
headers: {
'host': 'www.google.com'
},
strictSSL: false,
time: true,
gzip: true,
}, function (error, response, body) {
res.send(
{
'starts_at: ':{
'timings.socket':response.timings.socket,
'timings.lookup':response.timings.lookup,
'timings.connect': response.timings.connect,
'timings.response': response.timings.response,
'timings.end': response.timings.end
},
'duration: ':{
'timingPhases.wait': response.timingPhases.wait,
'timingPhases.dns': response.timingPhases.dns,
'timingPhases.tcp': response.timingPhases.tcp,
'timingPhases.firstByte': response.timingPhases.firstByte,
'timingPhases.download': response.timingPhases.download,
'timingPhases.total': response.timingPhases.total
}
});
});
});
module.exports = router;
输出在这里:
{
starts_at:
{
timings.socket: 3.725953000015579,
timings.lookup: 3.725953000015579,
timings.connect: 117.76885500003118,
timings.response: 692.0431389999576,
timings.end: 1220.5685750000412
},
duration:
{
timingPhases.wait: 3.725953000015579,
timingPhases.dns: 0,
timingPhases.tcp: 114.0429020000156,
timingPhases.firstByte: 574.2742839999264,
timingPhases.download: 528.5254360000836,
timingPhases.total: 1220.5685750000412
}
}
但是当我在chrome中搜索相同的查询时,会得到以下结果:
我不知道为什么这些数字大不相同。 tcp
中的nodejs
时间大约需要114毫秒,而在Chrome浏览器中,大约需要0毫秒。 chrome和nodejs
服务器也位于同一区域,但是time-to-first-byte
中的nodejs
比chrome慢约245毫秒。
我如何改善nodejs
使其更像chrome?