Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slow HTTPS request with Node JS and Express JS?

I have a question regarding the code i have posted below. Would it be normal for that /auth request to on avg. display 500 - 800 ms (waiting TTFB) when using the Google Chrome inspecting panel? I get the same result when using the module Request to do the https request. I on avg. get 60 ms when typing in https://graph.facebook.com directly in the browser.

Any help is appreciated!

app.get('/auth', function(req, res){

res.writeHeader(200, {'Content-Type': 'application/json'});

var options = {
    host: 'graph.facebook.com',
    port: 443,
    path: '/',
    method: 'GET',
};

https.get(options, function (resp) {
    var body = '';
    resp.on('data', function (chunk) {
        body += chunk;
    });

    resp.on('end', function () {
        console.log('Request ended.');
        res.end('Done');
    });
});
});

- Snapshots of timings -

Cached:

  • Stalled: 0.458 ms
  • Request sent: 0.286 ms
  • Waiting (TTFB): 782.409 ms
  • Content Download: 0.994 ms

After clearing browser data:

  • Stalled: 0.695 ms
  • DNS Lookup: 15.062 ms
  • Initial connection: 48.599 ms
  • SSL: 23.922
  • Request sent: 0.308
  • Waiting (TTFB): 645.622 ms
  • Content Download: 1.560 ms

.. and i use Nginx in front of node.


1 Answers

You are almost certainly spending all that time opening and negotiating SSL with Facebook. Your current setup will have to do that every time you send a request to your server. Generally you'd want to use something like https://www.npmjs.com/package/agentkeepalive to keep a connection pool of pre-negotiated SSL sockets. Browsers do this, which is why it is so much faster from your browser directly to facebook.

Create the agent outside your route, and then in your route, pass the agent as part of your options object.

like image 103
loganfsmyth Avatar answered Nov 24 '25 20:11

loganfsmyth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!