您的位置:首页 > 理论基础 > 计算机网络

Adding timeout support for http request in Node.JS

2013-05-18 13:26 465 查看
This is HttpUtil Module, you need "require" it before using the module.

var http= require("http");

var HttpUtil = function(){
var namespace = {
Get: function(options, timeout, callback){
var timer=null;
var req = null;
var hasCallBackCalled =false;
// set timeout, it will cancel the request if there is a timeout
timer = setTimeout(function(){
if(req!=null)
req.abort();
if(!hasCallBackCalled &&  callback){
hasCallBackCalled=true;
var errObj = new HttpResult(true, "Request Timeout!", null);
callback(errObj);
}
}, timeout);

req = http.get(options, function(res){

clearTimeout(timer);
var chunks=[],length=0;

res.on("data", function(chunk){
length += chunk.length;
chunks.push(chunk);
});

res.on("end", function(){
var data =chunks==null? null: chunks.toString();
if(!hasCallBackCalled && callback){
hasCallBackCalled=true;
var errObj = new HttpResult(false, null, data);
callback(errObj);
}
});

}).on("error", function(e){
clearTimeout(timer);
if(!hasCallBackCalled &&  callback){
hasCallBackCalled=true;
var errObj = new HttpResult(true, e.message, null);
callback(errObj);
}
});
},
Post: function(options, postData, timeout, callback){
var timer=null,
req = null,
hasCallBackCalled =false;

// set timeout, it will cancel the request if there is a timeout
timer = setTimeout(function(){
if(req != null)
req.abort();
if(!hasCallBackCalled &&  callback){
hasCallBackCalled=true;
var errObj = new HttpResult(true, "Request Timeout!", null);
callback(errObj);
}
}, timeout);

req= http.request(options, function(res){
//res.setEncoding('utf-8');
clearTimeout(timer);
var data = '';

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

res.on('end',function(){
if(!hasCallBackCalled && callback){
hasCallBackCalled=true;
var errObj = new HttpResult(false, null, data);
callback(errObj);
}
});

}).on('error',function(e){
clearTimeout(timer);
if(!hasCallBackCalled &&  callback){
hasCallBackCalled=true;
var errObj = new HttpResult(true, e.message, null);
callback(errObj);
}
});

req.write(postData);
}
};
return namespace;
}();

/* HttpResult is the object when finished the request.
hasError: Indicate whether there is an error when performing a request.
errorMessage: If hasError is true, you can get error message here.
data: If there is no error, you can get the data from the request.
*/
function HttpResult(hasError, errorMessage, data){
this.hasError=hasError;
this.errorMessage = errorMessage;
this.data=data;
}

/*Export the function to public*/
module.exports = HttpUtil;

Below is the usage for above module.

var HttpUtil = require("./HttpUtility");
var querystring =require("querystring");

function process(e){
console.log(e.data);
console.log(e.hasError);
console.log(e.errorMessage);
}
/********************this is getting data***********************************
var options={
host: "www.google.com",
port: 80
};

HttpUtil.Get(options, 5000, process);

/********************this is posting data***********************************/
var jsonData={
txtbill:"1234567"
};

var postData = querystring.stringify(jsonData);

var options={
host: "www.zto.cn",
method: "POST",
path: "http://www.zto.cn/bill.aspx",
headers:{
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};

HttpUtil.Post(options, postData, 5000, process);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐