node 发送get, post请求,支持http和https

// 通用get请求(http, https)
var sendGetRequest = function(ip, port, path, headers, type) {
	
	return new Promise((resolve, reject) => {
			var opt = {
				host: ip,  // 这里是ip(192.168.1.1)或者域名(mydomain.com),注意不能带http://或https://
			    port: port,
			    method: 'GET',
			    path: path,
			    headers: headers
			}
			var req = null;
			var rpcResult = '';
			var datas = '';
			
			if (type == "http") {
				req = http.request(opt, function(result) {
					result.on('data', function(data) {
						console.log(data);
						try {
							datas += data;  // 注意:返回json数据量大时,会截取分批返回
						} catch(e) {
							console.log(e);
						} 
				    }).on('end', function(){
				    	if (datas != undefined && datas != null) {
				    		rpcResult = JSON.parse(datas); 
					    	resolve(rpcResult);
				    	} else {
				    		// 请求获取不到返回值
				    		resolve("2");
				    	}
				    	
				    })
				});
			} else if (type == "https") {
				req = https.request(opt, function(result) {
					result.on('data', function(data) {
						try {
							datas += data;  // 注意:返回json数据量大时,会截取分批返回
						} catch(e) {
							console.log(e);
						} 
				    }).on('end', function(){
				    	if (datas != undefined && datas != null) {
				    		rpcResult = JSON.parse(datas); 
					    	resolve(rpcResult);
				    	} else {
				    		// 请求获取不到返回值
				    		resolve("2");
				    	}
				    	
				    })
				});
			}
			
			req.on('error', function (e) {  
				// request请求失败
			    console.log('请求失败: ' + e.message); 
			    reject("0");
			});  
			req.end();
	})
	
}

// 通用post请求(http,https)
var sendPostRequest = function(ip, port, path, headers, type, params) {
	return new Promise ((resolve, reject) => {
		data = JSON.stringify(params);
		var opt = {
		    host: ip,
		    port: port,
		    method: 'POST',
		    path: path,
		    headers: headers
//		    headers:{
//		        "Content-Type": 'application/json',
//		        "Accept": 'application/json',
//		        "Content-Length": data.length
//		    }
		}
		
		var request = null;
		var rpcResult = '';
		var datas = '';
		if (type == "http") {
			
			request = http.request(opt, function(result) {
				
				result.on('data',function(data) {
					try {
						datas += data;  // 注意:返回json数据量大时,会截取分批返回
					} catch(e) {
						console.log(e);
					}
			    }).on('end', function(){
			    	rpcResult = JSON.parse(datas)
			    	resolve(rpcResult);
			    });
			}).on('error', function(e) {
			    console.log("error: " + e.message);
			    reject(e);
			});
		} else if (type == "https") {
			
			request = https.request(opt, function(result) {
				result.on('data',function(data) {
					try {
						datas += data;  // 注意:返回json数据量大时,会截取分批返回
					} catch(e) {
						console.log(e);
					}
			    }).on('end', function(){
			    	rpcResult = JSON.parse(datas)
			    	resolve(rpcResult);
			    });
			}).on('error', function(e) {
			    console.log("error: " + e.message);
			    reject(e);
			});
		}

		// 发送请求(post请求需要写入参数)
		request.write(data);
		request.end();
	});
	
}

    

1. node发送get和post请求,注意opt里面的ip一定是单独的ip或者域名,不带http://和https://的。

2. headers的传入, 如果里面定义的key是变量,比如调用某个数据提供商的api,需要他们赋予调用权限的secretKey,比如阿里的是aliSecretKey, 京东的是jdSecretKey,这时可以通过[]定义对象的方式,以便方法可以通用。如:

var headers = {};
headers[secretKey] = secretValue

3. 返回json数据量大,会截取分批返回,所以在回调里定义了一个string执行++, 这个一定要注意

参考:

https://nodejs.org/api/http.html

https://nodejs.org/api/https.html

https://www.cnblogs.com/Gasg/p/8044889.html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章