title: Nodejs Socket hang up & ECONNRESET - HTTP post request from Meteor to Node js server
body: <p>I am using a node server to handle all my push notifications services like gcm and apn.</p>
  
  <p>I have 2 different servers. One is running Meteor and another is running Node.JS to handle push notifications. (Both are different servers)</p>
  
  <p>My main app runs on the Meteor server.</p>
  
  <p>I make an HTTP post request to the node.js server to send my notifications.</p>
  
  <p>Usually it works fine, but sometimes on the Meteor server I get this error whenever I call the node.js server:</p>
  
  <pre><code>socket hang up\n    at Object.Future.wait (/home/myPc/.meteor/packages/meteor-tool/.1.1.10.ki0ccv++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:398:15)\n    at Object.&lt;anonymous&gt; (packages/meteor/helpers.js:119:1)\n    at Object.HTTP.call (packages/meteorhacks_kadira/lib/hijack/http.js:10:1)\n    at Object.sendPushNotificationsMeteorServer (server/pushNotifications.js:249:1)\n    at server/classes/pushNotifications.js:244:1\n    at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)\n    at packages/meteor/timers.js:6:1\n    at runWithEnvironment (packages/meteor/dynamics_nodejs.js:110:1)\n    - - - - -\n    at createHangUpError (http.js:1473:15)\n    at Socket.socketOnEnd [as onend] (http.js:1569:23)\n    at Socket.g (events.js:180:16)\n    at Socket.emit (events.js:117:20)\n    at _stream_readable.js:944:16\n    at process._tickCallback (node.js:448:13)',
  details: { [Error: socket hang up] stack: [Getter] },
  data: { [Error: socket hang up] stack: [Getter] },
  user: null,
  userId: null,
  toString: [Function] },
  user: null,
  userId: null,
  toString: [Function] }
  </code></pre>
  
  <p><strong>OR</strong></p>
  
  <pre>
   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)
  
  </pre>
  
  <p>Here is my Node.JS server code:</p>
  
  <pre><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}));
  
  process.on('uncaughtException', function (err) {
  
    console.error(err);
    console.log("Node NOT Exiting...");
  });
  
  // All post requests
  app.post('/', function(req, res){
  
    try {
  
      var response = JSON.parse(req.body.pushNotificationApiParams);
      var callType = req.body.callType;
  
      switch (callType) {
          case 'systemPushNotifications':
              return pushNotificationClass.sendPushNotificationsV2(response);
          break;
      } 
    }
    catch(e){
  
      console.dir(e.stack);
      realFs.appendFile('errorLogs/'+helperClass.getCurrentDateFormated()+'.log', helperClass.formatLog('Exception in main Post Method  : '+e.stack) , function (err) {
        if (err) throw err;
      });
    }
  
    res.send("OK");
  });
  
  
  
  app.listen(port, function () {
    console.log('Listening at '+hostname+':'+port);
  });
  </code></pre>
  
  <p>And here is my code from Meteor side, where I am make HTTP post request to node js server:</p>
  
  <pre><code>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 });
  </code></pre>
  
  <p>Can anyone guide me in the right direction? Also I would really appreciate to know some good practices as well.</p>
  
  <p><strong>Also</strong></p>
  
  <p>There is one more issue I am facing. My node.js server exits after like 24 hours. I don't know why does that happens. It exits without any error or exception in the terminal console. I have to restart it each time.</p>