Last active
November 28, 2020 04:48
-
-
Save rezonn/96a2450d2089263fa01bf5ad3283be86 to your computer and use it in GitHub Desktop.
nodes fetch polyfill
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function fetch(url,options2) { | |
| var http = require('http'); | |
| if (url.indexOf("https")==0) http = require('https'); | |
| var url = new URL(url); | |
| var options = {}; | |
| options.host = url.host; | |
| options.path = url.pathname+url.search; | |
| for (var x in options2) { | |
| options[x]=options2[x]; | |
| } | |
| return new Promise(function (resolve, reject) { | |
| var req = http.request(options, (res) => { | |
| res.setEncoding('utf8'); | |
| var data = ""; | |
| res.on('data', function (chunk) { | |
| data += chunk; | |
| }); | |
| res.on("end", function () { | |
| resolve({ | |
| headers:res.headers, | |
| text:function(){return data} | |
| }); | |
| }); | |
| }).on('error', (e) => { | |
| reject(e); | |
| }); | |
| if (options2 && options2.body) req.write(options2.body); | |
| req.end(); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment