简体   繁体   English

套接字在http.request上挂接到Node.js MongoDb服务器

[英]Socket hang up on http.request to a Node.js MongoDb server

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: 在我的本地PC中,我设置了一个向Node.js RESTful MongoDb服务器发出请求的Node.js代理服务器,一个在端口8080,另一个在端口3000。代理在RESTful服务器日志中可以看到所有查询已正确发送回代理,但代理挂断并抛出此错误:

    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: 这些是发送到MongoDB服务器的选项:

    {
      "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"
               }
    }

I found a solution replacing http.request() with http.get() , apparently the socked hang up because the socket did not sent the connection end event within the timeout period. 我找到了一个解决方案,将http.request()替换为http.get() ,显然是挂断了,因为套接字未在超时时间内发送连接结束事件。 A similar issue here: NodeJS - What does "socket hang up" actually mean? 这里有一个类似的问题: NodeJS-“套接字挂起”实际上是什么意思?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM