2013-08-18 26 views
0

我在发送邮件请求时收到“套接字挂断”错误。我无法解决它。套接字挂断节点0.8.17

sparqlQ = getSPARQLPrefix() + query_string; 
    console.log(sparqlQ) 
    var options = { 
    host: process.env['SESAME_HOST'], 
    port: process.env['SESAME_PORT'], 
    method: 'POST', 
    path: 
     '/openrdf-sesame/repositories/myReo?update=' + 
     encodeURIComponent(sparqlQ) + 
     '&content-type=application/sparql-results+json', 
    headers: { 
     'Content-Type': 'application/x-www-form-urlencoded', 
     'Accept': 'application/sparql-results+json', 
    }, 
    }; 

    var req = http.request(options, function(res) { 
    var data = ""; 
    res.on('data', function (chunk) { 
     data += chunk; 
    }); 
    res.on('error', function (error) { 
     console.log(error) 
    }); 
    res.on('end', function() { 
     console.log(data) 
     req.end(); 
     callback(null); 
    }); 
    }).on('error', function(e) { 
    console.alert("Error getting sesame response [%s]", e.message); 
    req.end(); 
    callback(e.message); 
    return 
    }); 

我在做什么错?请帮忙!

+0

升级您的节点版本。这就是你需要做的。 –

+0

或者在req.socket –

+0

上侦听错误,如果我使用req.socket('error',..)...那么它说TypeError:Object#没有方法'socket' – GJain

回答

1

这里提到两件事。


你是不是在你的http request调用req.end()

参考this documentation在node.js的http模块上。

With http.request() one must always call req.end() to signify that you're done with the request - even if there is no data being written to the request body.


req.error事件您呼叫 console.alert我认为

应该是console.log


这里是一个示例代码

http = require("http"); 
var options = { 
    host: "localhost", 
    port: 80, 
    method: 'POST'  
}; 

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

    var data = ""; 
    res.on('data', function (chunk) { 
     data += chunk; 
    }); 
    res.on('error', function (error) { }); 
    res.on('end', function() { 
     console.log(data) 
     req.end(); 
     console.log(null); 
    }); 

}).on('error', function(e) { 

     console.log("Error getting sesame response [%s]", e.message); 
     req.end(); 
     console.log(e.message); 
     return 

}); 

req.end();