2013-08-18 56 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();