nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
105.13k stars 28.48k forks source link

Error: read ECONNRESET - HTTP post request from Meteor to Node js server #10422

Closed MartiniHenry1988 closed 7 years ago

MartiniHenry1988 commented 7 years ago

I am using a node server to handle all my push notifications services like GCM and APN.

I have 2 different servers. One is running the Meteor & another is running the Node Js to handle push notifications.

My main application is build on Meteor server.

I make an HTTP post request to nodejs server to send my notifications.

Usually it works fine, but sometimes on Meteor server I get this error whenever I call the nodejs server:

Error: read ECONNRESET at Object.Future.wait (/home/mbm/.meteor/packages/meteor-tool/.1.1.10.12ml1tp++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:398:15) at Object.call (packages/meteor/helpers.js:119:1) at Object.sendHttpCall (server/pushNotifications.js:249:1) at server/pushNotifications.js:244:1 at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1) at packages/meteor/timers.js:6:1 at runWithEnvironment (packages/meteor/dynamics_nodejs.js:110:1) - - - - - at errnoException (net.js:905:11) at TCP.onread (net.js:559:19)

Here is my Node Js server code:

realFs                = require('fs');
var gracefulFs        = require('graceful-fs');
gracefulFs.gracefulify(realFs);

var http              = require('http');
var express           = require('express');
var app               = express();

var path              = require("path");

configClass           = require('./classes/config.js').configClass;
helperClass           = require('./classes/helper.js').helperClass;
pushNotificationClass = require('./classes/pushNotification.js').pushNotificationClass;

var hostname          = 'http://localhost'; 
var port              = 6000;

var bodyParser        = require('body-parser');

nodeGcm               = require('node-gcm');
apn                   = require('apn');
apnService            = new apn.Connection(helperClass.getAPNOptions());

// -- BODY PARSER -- //
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));

// All post requests
app.post('/', function(req, res){

  try {

    var response = JSON.parse(req.body.pushNotificationApiParams);
    var callType = req.body.callType;

    res.writeHead(200, {'Content-Type': 'text/html'});

    switch (callType) {
        case 'systemPushNotifications':
            return pushNotificationClass.sendPushNotificationsV2(response);
        break;
    } 
    res.end('thanks');
  }
  catch(e){

    realFs.appendFile('errorLogs/'+helperClass.getCurrentDateFormated()+'.log', helperClass.formatLog('Exception in main Post Method  : '+e.stack) , function (err) {
      if (err) throw err;
      //console.log('The "data to append" was appended to file!');
    });
  }
});

app.listen(port);
console.log('Listening at '+hostname+':'+port);

And here is my code from Meteor side, where I am make HTTP post request to node js server:

var headers = {
   'Content-Type' : 'application/x-www-form-urlencoded'
};

var postFields = {
   callType : 'systemPushNotifications',
   pushNotificationApiParams  : JSON.stringify(pushNotificationApiParams)   // contains push notifications data                 
};

HTTP.call("POST", 'http://localhost:6000', { params:postFields, headers:headers });

Can anyone guide me in the right direction? Also I would really appreciate to know some good practices as well.

bnoordhuis commented 7 years ago

Do you have a test case that uses only built-in modules? If yes, please post it. If not, you should close this and move to https://github.com/nodejs/help/issues or whatever the most appropriate bug tracker is (meteor, node-gcm, apn?). This bug tracker is only for reporting issues with node.js core.

MartiniHenry1988 commented 7 years ago

@bnoordhuis Actually this is a nodejs issue. Because it gives ECONNRESET. It means Nodejs server is denying my requests sometimes. And about the node-gcm, apn I have tested them. they work fine on meteor server.

bnoordhuis commented 7 years ago

Okay, then please post a core modules-only test case. No one is going to take a look at it as long as it uses third-party modules. You should try to exclude meteor as well.

MartiniHenry1988 commented 7 years ago

@bnoordhuis I am concerned with this piece of code on my nodejs server:

// All post requests
app.post('/', function(req, res){

  try {

    var response = JSON.parse(req.body.pushNotificationApiParams);
    var callType = req.body.callType;

    res.writeHead(200, {'Content-Type': 'text/html'});

    switch (callType) {
        case 'systemPushNotifications':
            return pushNotificationClass.sendPushNotificationsV2(response);
        break;
    } 
    res.end('thanks');
  }
  catch(e){

    realFs.appendFile('errorLogs/'+helperClass.getCurrentDateFormated()+'.log', helperClass.formatLog('Exception in main Post Method  : '+e.stack) , function (err) {
      if (err) throw err;
      //console.log('The "data to append" was appended to file!');
    });
  }
});

As you see res.writeHead(200, {'Content-Type': 'text/html'}); & res.end('thanks'); . I assume the are not writing and returning my response properly sometimes due to the async code running between

bnoordhuis commented 7 years ago

Closing for lack of reproduction. @MartiniHenry1988 If you still wish to pursue this, then please post a standalone test case.