proxy.js
1.4 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
//RESTful API client 代理
var http = require("http");
var uuid = require('uuid/v4');
var URL = require('url');
exports = module.exports = function (logger, settings) {
/*
* var options={
'host': 'xxx.xxx.xxx.xxx',
'port': 'xxxxx',
'path': '/uaa/v1/xxxxx',
'method': 'POST',
'agent': false,
'headers': {
"Accept": "application/json",
"Content-Type": "application/json",
'User-Agent': 'Request for Express',
'Authorization':"Bearer " + token
}
}
*/
var client = function (options, success, fail) {
return new Promise(function (resolve, reject) {
var postData = !options.params ? '' : 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(e);
});
httpRequest.write(postData);
httpRequest.end();
});
};
return client;
};
exports['@singleton'] = true;
exports['@require'] = ['igloo/logger', 'igloo/settings'];