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.
Below is the usage for above 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);
相关文章推荐
- Using HTTP Proxy in Node.js with request package
- How to disable 'withcredentials' in HTTP header with node.js and Request package?
- Nodejs HTTP请求的超时处理(Nodejs HTTP Client Request Timeout Handle)
- SSM报错:No mapping found for HTTP request with URI [/smbms/js/echarts.js] in DispatcherServlet with na
- Node.js Request Module…sending json in body for api request with put
- Nodejs HTTP请求的超时处理 Nodejs HTTP Client Request Timeout Handle
- why request event is fired before data event in http modlues of node.js.
- No mapping found for HTTP request with URI [/operation-platform/js/displaytag.js] in DispatcherServl
- warn: Appium support for versions of node < 0.12 has been deprecated and will be removed in a future
- No mapping found for HTTP request with URI [/cwgl/department/index] in DispatcherServlet with name
- Node.js http.request()返回响应出现乱码的解决方案
- springmvc No mapping found for HTTP request with URI in Dispatc
- Spring : No mapping found for HTTP request with URI in DispatcherServlet with name "..."
- No mapping found for HTTP request with URI [/zjic] in DispatcherServlet
- No mapping found for HTTP request with URI in DispatcherServlet with name 'XXX'
- No mapping found for HTTP request with URI [/helloMvc/welcome/hello] in DispatcherServlet
- No mapping found for HTTP request with URI [/SuperPowerfulProject/js/jquery.dataTables.js等静态资源加载
- scala: How to write a simple HTTP GET request client in Scala (with a timeout)
- NodeJS http.ClientRequest & http.ClientResponse
- No mapping found for HTTP request with URI [/Demo/WEB-INF/page/login.jsp] in DispatcherServlet with