How to recover from Node.js request pipe with ECONNRESET error? - node.js

My site is all in HTTPS. When I show some pictures from other non-HTTPS sites, I rewrite the request from my server and pipe the response to the client as follows.
request.get(imageUrl)
.on('response', function(response) {})
.on('error', function(err) {
console.log(err);
}).pipe(res);
Sometimes it throws the error of
{ [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }.
So how can I recover from the error to make my site continue to serve?

Related

Can't make a POST request on Azure - EACCES

I have a Node application hosted in Azure as a Web App Bot. When I try to make a POST request with axios from that application, it gives me the following error:
{ Error: connect EACCES 172.30..etc etc
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
errno: 'EACCES',
code: 'EACCES',
syscall: 'connect',
address: '172.30..etc,etc',
port: 443,
config:
...
Locally, testing with Bot Framework Emulator it works like a charm.
Request:
let reqConfig = {
headers: {
"Authorization": '123etc etc',
"Content-Type": "application/json"
}
}
axios.post("https://_______", body, reqConfig).then(r => {
console.log(r);
}).catch(e => {
console.log('ERR: ', e);
})

Problem with Facebook Graph API in Node.js application

I've created the node.js application which is connected with Facebook Graph API. The problem is that when I am using request to get the data from the URL, sometimes I am not receiving the data but this error message:
{
Error: connect ETIMEDOUT 31.13.81.9:443
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
code: 'ETIMEDOUT',
errno: 'ETIMEDOUT',
syscall: 'connect',
address: '31.13.81.9',
port: 443
}
When I paste the URL to the browser I am receiving the data from API without any problem.
The request which gets the Facebook API data
request({url:`https://graph.facebook.com/v3.2/me?fields=posts%7Bcomments%7Blike_count%7D%2Cshares%2Ccreated_time%2Csource%2Clikes.summary(true)%7D%2Cfan_count&period=day&since=${selectedDateFrom}&until=${selectedDateTo}&access_token=${accessToken}`,
json: true
}, (err, response, body) => {
if (err) {
console.log(err)
} else {console.log(body);}
I wonder why the problem is happening in my application, and why in the browser everything is fine?

How to avoid a NodeJS ECONNRESET error?

I have an app running which is doing every 20 to 30 minutes a http.request. When I do these requests every 10 seconds or so, it works just fine, but when there is a gap of 30 minutes between the requests, I get the following error message.
{ [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }
Now I think, this is a timeout error. The connection dies in the background and when the next request take place, it throws this error since the connection is dead.
My idea now is to close the connection after each request and re-establisch the connection on every new request. But I have no idea how to accomplish this in NodeJS.
UPDATE
I found out that the problem appears, when I first GET than POST and again GET.
Here ist the code for GET and POST
https.request({ method: 'GET'..., function(res) {
}).on('error', function(e) {
throw 'error';
}).end();
var request = https.request({ method: 'POST'..., function(res) {
}).on('error', function(e) {
throw 'error';
});
request.write(query);
request.end();

Got "EAGAIN" error when sending request with "request module" (node.js module)

I have the same code for my Windows and Linux. It works on Windows but failed with my Linux.
Code:
request({url:target_url}, // ANY URL WILL FAIL
function (err, response, body) {
console.log(err);
Out put error for this in my console:
{ [Error: connect EAGAIN] code: 'EAGAIN', errno: 'EAGAIN', syscall: 'connect' }
I guess this is not an issue with the request module but i don't know where to get help. Thanks for any kind of tips!

HTTP 400: Bad Request error in ADFS HTTPS Request

I am writing a Node.js app and am trying to integrate an ADFS server to get authentication. For that, I am using wstrust-client, and using the ADFS Server URL as my endpoint. My code so far is:
app.get('/login', function(req, res) {
trustClient.requestSecurityToken({
scope: 'https://mycompany.com',
username: "username",
password: "password",
endpoint: 'https://[adfs server]/adfs/services/trust/13/usernamemixed'
}, function (rstr) {
// Access the token
var rawToken = rstr.token;
console.log('raw: ' + rawToken);
}, function(error) {
console.log(error)
});
});
I am requesting https through wstrust-client
My code in wstrustclient.js so far is:
var req = https.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function(data) {
console.log("Entered res")
var rstr = {
token: parseRstr(data),
response: res,
};
callback(rstr);
});
});
req.write(message);
req.end();
req.on('error', function (e) {
console.log("******************************");
console.log(e);
console.log("******************************");
However, it is throwing this error:
******************************
{ [Error: UNABLE_TO_VERIFY_LEAF_SIGNATURE]
stack: 'Error: UNABLE_TO_VERIFY_LEAF_SIGNATURE\n
at SecurePair.<anonymous> (tls.js:1253:32)\n
at SecurePair.EventEmitter.emit (events.js:91:17)\n
at SecurePair.maybeInitFinished (tls.js:865:10)\n
at CleartextStream.read [as _read] (tls.js:416:15)\n
at CleartextStream.Readable.read (_stream_readable.js:231:10)\n
at EncryptedStream.write [as _write] (tls.js:329:25)\n
at EncryptedStream.Writable.write (_stream_writable.js:176:8)\n
at write (_stream_readable.js:496:24)\n
at flow (_stream_readable.js:506:7)\n
at Socket.pipeOnReadable (_stream_readable.js:538:5)' }
******************************
******************************
{ [Error: read ECONNRESET]
stack: 'Error: read ECONNRESET\n
at errnoException (net.js:846:11)\n
at TCP.onread (net.js:508:19)',
code: 'ECONNRESET',
errno: 'ECONNRESET',
syscall: 'read' }
******************************
When I browse the same endpoint URL in a browser, it throws HTTP 400: Bad Request
I know that it's an SSL type error, and that it's from the server-side. However, I don't know why it's throwing the error and what might be wrong server-side. What do I need to change?
As per the OpenSSL manual here:
21 X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: unable to verify the
first certificate no signatures could be verified because the chain
contains only one certificate and it is not self signed.
With that in mind, it seems that you may need to sign your certificate.

Resources