3

I am creating a website using nodejs. In a page of my website, i request to google then parse its results and send them to the user. I use request module of npm to request to google. request module has a flag called time that if i set it to true then request gives me details of timing of the request. I request to google like this:

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;

And the output is here:

{
    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
        }
}

But when i search the same query in chrome i get these results:

enter image description here

I do not know why these numbers are very different. tcp time in nodejs takes about 114 miliseconds but in chrome it takes about 0 milisecond. Also both chrome and nodejs server are in a same region but time-to-first-byte in nodejs is slower than chrome about 245 miliseconds.

How can i improve nodejs to be like chrome?

hamid
  • 694
  • 1
  • 8
  • 20

0 Answers0