mirror of
https://github.com/danbulant/bpm
synced 2026-05-19 04:08:47 +00:00
31 lines
No EOL
747 B
JavaScript
31 lines
No EOL
747 B
JavaScript
const http = require("http");
|
|
const https = require("https");
|
|
/**
|
|
* @param {String} path
|
|
*/
|
|
module.exports = (path, options = { method: "GET" }) => {
|
|
return new Promise((res, rej) => {
|
|
if(path.startsWith("http://")){
|
|
var handler = http;
|
|
} else if(path.startsWith("https://")){
|
|
var handler = https;
|
|
} else {
|
|
return rej(Error("Unsupported protocol"));
|
|
}
|
|
|
|
handler.get(path, options, (resp) => {
|
|
let data = '';
|
|
|
|
resp.on('data', (chunk) => {
|
|
data += chunk;
|
|
});
|
|
|
|
resp.on('end', () => {
|
|
res(data);
|
|
});
|
|
|
|
}).on("error", (err) => {
|
|
rej(err);
|
|
});
|
|
});
|
|
} |