-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.js
More file actions
64 lines (56 loc) · 2.12 KB
/
request.js
File metadata and controls
64 lines (56 loc) · 2.12 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
59
60
61
62
63
64
const https = require('https');
const http = require('http');
const streamConsumers = require('node:stream/consumers');
const contentType = require(AppPath+'/content_type');
const req = {
get(url) {
return req.request(url,{resType:'text'});
},
post(url ,postData) {
return req.request(url,{options:{ method: 'POST' },postData:postData} );
},
json(url,postData) {
if(typeof postData !== "string")postData = JSON.stringify(postData);
return req.request(url, {options:{
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
},postData:postData});
},
request(url, args ,headers) {
const {options ,postData ,resType} = Object.assign({options:null,postData:null,resType:null}, args || {});
const client = /^https/i.test(url) ? https : http;
return new Promise( (resolve, reject) => {
let req = client.request(url, Object.assign({ timeout: 5000 }, options || {}), (res) => {
if (res.statusCode !== 200){
console.log(url);
reject(res.statusMessage,{code:res.statusCode});
}
streamConsumers[resType || contentType(res)](res).then(data=>{
if(typeof headers == 'object')Object.assign(headers , res.headers);
resolve(data);
}).catch(e=>{reject(e);});
// var bData = Buffer.alloc(0);
// res.on('data', (chunk) => {
// bData = Buffer.concat([bData, chunk]);//streamConsumers.buffer(stream)
// });
// res.on('end', () => {
// resolve(bData);
// });
});
req.on('error', (e) => {
console.log(url);
reject(e);
});
req.on('timeout', () => {
req.destroy(new Error('timeout:'+url));
});
if(postData){
req.write(postData);
}
req.end();
});
}
};
module.exports = req;