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:
After clearing browser data:
.. and i use Nginx in front of node.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With