1

In my local pc I've set up a Node.js proxy server who makes request to a node.js RESTful MongoDb server, one is at port 8080 and the proxy at port 3000. I can see in the RESTful server log that all the queries are sent back correctly to the proxy, but the proxy hang up throwing this error:

    events.js:72
            throw er; // Unhandled 'error' event
                  ^
    Error: socket hang up
        at createHangUpError (http.js:1472:15)
        at Socket.socketOnEnd [as onend] (http.js:1568:23)
        at Socket.g (events.js:180:16)
        at Socket.EventEmitter.emit (events.js:117:20)
        at _stream_readable.js:920:16
        at process._tickCallback (node.js:415:13)

this is how I built my proxy request :

      var proxy = function(req, res, next) {
        try {
          var options = mapRequest(req);    
          var dbReq = http.request(options, function(dbRes) {
            var data = "";
            res.headers = dbRes.headers;
            dbRes.setEncoding('utf8');

            dbRes.on('data', function(chunk) { 
              data = data + chunk;
             });

            dbRes.on('end', function() {
              res.header('Content-Type', 'application/json');
              res.statusCode = dbRes.statusCode;
              res.httpVersion = dbRes.httpVersion;
              res.trailers = dbRes.trailers;
              res.send(data);
              res.end();
            });

          });

          dbReq.end(JSON.stringify(req.body));

        } catch (error) {
          console.log('ERROR: ', error.stack);
          res.json(error);
          res.end();
        }
       };

and these are the options sent to the MongoDB server:

    {
      "hostname":"127.0.0.1",
      "path":"//databases/db1/collections/documents?apiKey=134557676&l=5&sk=0",
      "method":"GET",
      "port":"8080",
      "headers":{
                "host":"127.0.0.1",
                "connection":"keep-alive",
                "accept":"application/json, text/plain ",
                "x-xsrf-token":"VPDlgN2iMWU2IXPIPH0aiwS5",
                "user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36",
                "dnt":"1",
                "referer":"http://localhost:3000/documents",
                "accept-encoding":"gzip,deflate,sdch",
                "accept-language":"it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4",
                "cookie":"XSRF-TOKEN=VPDlgN2iMWU2IXPIPH0aiwS5; connect.sess=s%3Aj%3A%7B%22passport%22%3A%7B%22user%22%3A%225298bfa9e4b070e1c60fd84f%22%7D%2C%22_csrf%22%3A%22VPDlgN2iMWU2IXPIPH0aiwS5%22%7D.wc85bNSpJIl7KnCHOUXiG5V2e7SI9XR9EctByTtqhu4"
               }
    }

1 Answer 1

2

I found a solution replacing http.request() withhttp.get(), apparently the socked hang up because the socket did not sent the connection end event within the timeout period. A similar issue here: NodeJS - What does "socket hang up" actually mean?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.