forked from skt-smartfleet/device-simulator-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise-http.js
45 lines (43 loc) · 1.18 KB
/
promise-http.js
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
var http = require('http');
var colors = require('colors');
/** http request를 promise로 사용하기 위한 함수 */
exports.request = function(args){
args = args || {};
return new Promise(function(resolve, reject){
var req = http.request(args.options,function(res){
console.log(colors.blue('Sending a POST message to T-RemotEye Platform'));
console.log(args.body);
console.log('');
res.setEncoding('utf8');
res.on('data', function (chunk) {
resolve({
statusCode : res.statusCode,
headers : res.headers,
data: chunk
});
});
if(res.statusCode!==200 && res.statusCode!==201 && res.statusCode!==409){
reject({
statusCode : res.statusCode,
error: 'statusCode is '+res.statusCode,
options: args.options,
responseHeader : res.headers
});
}
});
req.on('error',function(e){
reject({
error: e,
options: args.options
});
});
if(args.body){
if(typeof args.body == 'string'){
req.write(args.body);
}else{
req.write(JSON.stringify(args.body));
}
}
req.end();
});
}