mirror of
https://github.com/danbulant/bpm
synced 2026-06-14 03:51:07 +00:00
Simplify requests
This commit is contained in:
parent
6c6cfcd0ef
commit
8be20adb9d
1 changed files with 31 additions and 0 deletions
31
modules/requests.js
Normal file
31
modules/requests.js
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
const http = require("http");
|
||||
const https = require("https");
|
||||
/**
|
||||
* @param {String} path
|
||||
*/
|
||||
module.exports = (path) => {
|
||||
return new Promise((res, rej) => {
|
||||
if(path.startsWith("http://")){
|
||||
var handler = http;
|
||||
} else if(path.startsWith("https://")){
|
||||
var handler = https;
|
||||
} else {
|
||||
rej(Error("Unsupported protocol"));
|
||||
}
|
||||
|
||||
handler.get(path, (resp) => {
|
||||
let data = '';
|
||||
|
||||
resp.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
resp.on('end', () => {
|
||||
res(data);
|
||||
});
|
||||
|
||||
}).on("error", (err) => {
|
||||
rej(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Reference in a new issue