I need to send a post request to a form that is using 'https://' many times, as my script goes through a list of a lot of different words (the list is quite large). It works great only up to some point, and then it stops and I get the error:
events.js:291
throw er;
Error: connect ETIMEDOUT some.ip.addr.es:443
at tcp.connectWrap.afterConnect [as oncomplete] (net.js:1145:16)
'some.ip.addr.es' is not what shows in the error, I just changed the ip it showed.
I'm guessing I get the error because either I do the post request the wrong way in a FOR loop or the loop simply just does it too fast and too many times and it makes the connection time out. Because it works fine when it loops through a smaller list of words.
Here's my code:
var querystring = require('querystring');
var https = require('https');
const fs = require('fs');
var lineReader = require('line-reader');
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('first item: ', first_parameter => {
doWork(first_parameter);
readline.close();
});
async function doWork(first_parameter) {
if (first_parameter.length == 0) {
console.log("You didn't specify the first item...")
} else {
lineReader.eachLine('myList.txt', function(line, last) {
post_php(line, first_parameter)
})
}
}
async function post_php(x, first_parameter) {
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
var post_data = querystring.stringify({
'first_item': first_parameter,
'second_item': x,
});
var post_options = {
host: 'some.website.com',
port: '443',
path: '/directory/form.php',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(post_data)
}
};
var post_req = https.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log("Passing Parameter: " + x)
});
})
post_req.write(post_data);
post_req.end();
};
This code works good with a small list of words and throws no errors. With a bigger one, eventually I get hit with the ETIMEDOUT error and I'm assuming it's because either:
1. The loop is doing too many requests in too quick time and the server can't handle it (if so, how can I slow the loop down?)
OR
2. I'm not supposed to be sending https request to the php form this way and there is a better way to do that.
Please let me know if you can think of any possible fix for this. Thank you in advance.
What I have tried:
I tried to create a sleep() function and slow the loop down so that the https request isn't done too many times and too fast, but the sleep doesn't seem to affect anything and the loop still runs really fast.