Simplify requests

This commit is contained in:
danbulant 2020-02-19 17:11:09 +01:00
parent 6c6cfcd0ef
commit 8be20adb9d

31
modules/requests.js Normal file
View 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);
});
});
}