Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
MartiniHenry1988 opened this issue Dec 23, 2016 · 5 comments
Closed
Labels
invalid Issues and PRs that are invalid. question Issues that look for answers.

Comments

@MartiniHenry1988
Copy link

MartiniHenry1988 commented Dec 23, 2016

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
Copy link
Member

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
Copy link
Author

@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
Copy link
Member

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
Copy link
Author

@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

@mscdex mscdex added the question Issues that look for answers. label Dec 23, 2016
@bnoordhuis
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid Issues and PRs that are invalid. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

3 participants