【问题标题】:socket hanging and node crashing套接字挂起和节点崩溃
【发布时间】:2013-06-14 09:41:59
【问题描述】:

我有一个节点应用程序,可以将数据发布到远程 api 并获取响应。它工作正常,但有时节点服务器崩溃并生成以下错误:

 events.js:71
throw arguments[1]; // Unhandled 'error' event
^
Error: socket hang up
at createHangUpError (http.js:1264:15)
at Socket.socketCloseListener (http.js:1315:23)
at Socket.EventEmitter.emit (events.js:126:20)
at Socket._destroy.destroyed (net.js:358:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)

我用谷歌搜索发现它是由于一些超时的事情而发生的,但我不确定如何克服这个问题。 这是我的 server.js 中所需的代码:

if(request.body.company=="CQ")
    {
        var postData={firstName:request.body.firstname,lastName:request.body.lastname,Email:request.body.email,DayPhoneNumber:request.body.daytimePhone,Address1:request.body.addressOne,city:request.body.city,state:request.body.State,postalCode:request.body.zip,AreaOfIntrest:request.body.areaOfInterest,VendorLocationId:"38404",Notes:request.body.Notes,GraduationYear:request.body.graduationYear,AffiliateLocationId:"12345",LocationId:"12345",CampaignId:"12345"};

        var options={hostname:'webservices.someapi.com', path:'/ILM/Default.ashx?'+qs.stringify(postData), method:'GET'};

        var req = http.request(options, function(res) {

        res.on('data', function (chunk) {

        edModel.find({$and:[{daytimePhone:request.body.daytimePhone},{company:"CQ"}]},function(err,count){
        if(count.length==0)
        {
                var sr='RESPONSE: ' + chunk;
                if(sr.indexOf('status="Error"')==-1)
                {
                    request.body.leadid=sr;
                    var sr=sr.slice(sr.indexOf("leadid"));
                    sr=sr.slice(0,sr.indexOf(">"));

                    edDoc=new edModel(request.body);
                    edDoc.save();       
                    response.send({response:sr});
                }
                else
                {
                    response.send({response:sr});
                }
        }
        else
        {
            response.send({response:"<span style='color:red'>Duplicate Lead.<br> A lead with this number already exists in our database</span>"});
        }

    });


        });

        });

        // write data to request body
        req.write('data\n');
        req.end();
    }

我在 server.js 文件中有几个这样的 if else 条件。

【问题讨论】:

    标签: node.js sockets http mongodb


    【解决方案1】:

    在节点 0.8.20 中有一个关于该问题的错误。尝试使用 http.get 而不是 http.request。或者如果您使用该版本,则不要使用 0.8.20。

    【讨论】:

    • 您能否提供此错误的链接以供将来参考?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 2016-09-06
    • 2015-05-03
    相关资源
    最近更新 更多