乐趣区

关于javascript:node-封装-http请求

node 封装 http 申请

var http = require("http");
var urlUtil = require('url');
var file = require("./file");
var querystring = require('querystring');
var HttpUtil = {
    //get 提交 url,返回 html 数据
    get : function(url,success,error){http.get(url,function(res){
            var result = "";
            res.setEncoding("UTF-8");
            res.on("data",function(data){result += data;});
            res.on('error',error);
            res.on('end',function(){success(result);
            });
        }).on('error',this.requestError);
    },
    post : function(hostname,port,path,body,acceptType,contentType,success,error){
        var bodyString = "";
        if(body!=null && contentType == "application/json"){bodyString = JSON.stringify(body);
        }
        else if(body!=null && contentType == "application/x-www-form-urlencoded"){bodyString = querystring.stringify(body);
        }
        var opts = {
            hostname : hostname,
            port : port,
            path : path,
            method: 'post',
            headers : {
                'Accept':acceptType,
                'Content-Type':contentType,
                'Content-Length':bodyString.length
            }
        }
 
        var req = http.request(opts,function(res){
            
            var result = "";
            res.setEncoding("UTF-8");
            res.on("data",function(data){result += data;});
            res.on('error',error);
            res.on('end',function(){success(result);
            });
 
        });
        req.on('error',this.requestError);
        file.writeInFile(req);
        req.write(bodyString);
        req.end();},
    // 提交表单参数,并返回 html 内容
    postAndReturnHtml : function(url,body,success,error){var urlConfig = urlUtil.parse(url);
        var contentType = "application/x-www-form-urlencoded";
        var acceptType = "text/html";
        this.post(urlConfig.hostname,urlConfig.port,urlConfig.path,body,acceptType,contentType,success,this.responseError);
    },
    //get 提交 url 参数,并返回 json 数据
    getAndReturnJson : function(url,success,error){this.get(url,function(data){var data = JSON.parse(data);
            success(data);
        },this.responseError(error));
    },
    // 提交 json 参数,并返回 json
    postAndReturnJson : function(url,body,success,error){
        var contentType = "application/json";
        var acceptType = "application/json";
        var urlConfig = urlUtil.parse(url);
        this.post(urlConfig.hostname,urlConfig.port,urlConfig.path,body,acceptType,contentType,function(data){var data = JSON.parse(data);
            success(data);
        },this.responseError(error));
    },
    requestError : function(error){console.log("申请失败 --"+error.message);
    },
    responseError : function(error){return  error || function(e){console.log("响应失败 --"+e.message);
        };
    }
}
 
module.exports = HttpUtil;

更多文章

退出移动版