I am using Node.js (version 0.10.28) to make an http.request
to my Ruby API for a large amount of data (21,000 rows from a PostgreSQL database). The problem is that the request seems to time out and return a 404 error after 1 minute. I know this is true as the Ruby API returns 61 seconds for the request, and I time how long the Node's request takes (results in 60 seconds and the 404). However, if I wget
or use jQuery's $.ajax
with a timeout of 4 minutes, I can get my 21,000 rows. So clearly it can't be a 404, as other ways of getting the data work.
I'm a little confused too because everywhere I look, the http.request
isn't supposed to time out until after 2 minutes, according to:
I have tried several things to get this working, including: setting express's middleware; listening for the socket timeout and resuming the request; and setting the timeout to 0, so there's no timeout. Unfortunately, none of these ways worked, or at least from what I've understood.
For some clarity, here's my code... setting that limit means return only 18,000 records, and that seems to be the cut off point for the API taking longer than 60 seconds:
var http = require('http');
var options = {
path: '/api/records.json?limit=18000',
method: 'GET',
host: 'localhost',
requestCert: true,
rejectUnauthorized: false
};
var req = http.request(options, function(res) {
var endDate = new Date();
console.log('done', endDate - startDate);
var output = [];
res.on('data', function(chunk) {
output.push(chunk);
});
res.on('end', function() {
var data = output.join('');
console.log(data);
return {data: data, success: true};
});
});
req.on('socket', function(socket) {
socket.setTimeout(0); // no timeout
socket.on('timeout', function() {
socket.resume(); // tried resuming the timeout
});
});
req.end();
var startDate = new Date();
console.log('starting', startDate);
The API URL is valid and works, as I've verified that through wget
and $.ajax
, so how do I fix the timeout issue in Node?
UPDATE
Upon further inspection, if I take out socket.setTimeout(0);
and try to get into the socket.on('timeout'
, I don't actually get into the callback... which is odd, but it's always 60 seconds before I get the 404