From 8be20adb9d062b2070722012a63445e44e66fa01 Mon Sep 17 00:00:00 2001 From: danbulant Date: Wed, 19 Feb 2020 17:11:09 +0100 Subject: [PATCH] Simplify requests --- modules/requests.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 modules/requests.js diff --git a/modules/requests.js b/modules/requests.js new file mode 100644 index 0000000..b3e72d5 --- /dev/null +++ b/modules/requests.js @@ -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); + }); + }); +} \ No newline at end of file