proxy.js
1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//RESTful API client 代理
var http = require("http");
var uuid = require('uuid/v4');
var URL = require('url');
exports = module.exports = function (logger, settings) {
function addParams(params) {
var postData = !params ? '' : JSON.stringify(params || {});
return postData;
}
/*
* var options={
'host': 'xxx.xxx.xxx.xxx',
'port': 'xxxxx',
'path': '/uaa/v1/xxxxx',
'method': 'POST',
'params':{},
'agent': false,
'headers': {
"Accept": "application/json",
"Content-Type": "application/json",
'User-Agent': 'Request for Express',
'Authorization':"Bearer " + token
}
}
*/
var client = function (options) {
return new Promise(function (resolve, reject) {
var postData = addParams(options.params);
var httpRequest = http.request(options, function (res) {
var _data = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
_data += chunk;
});
res.on('end', function () {
resolve(_data, res);
});
});
httpRequest.on('error', function (e) {
reject(_data, e);
});
httpRequest.write(postData);
httpRequest.end();
});
};
return client;
};
exports['@singleton'] = true;
exports['@require'] = ['igloo/logger', 'igloo/settings'];