Last active
November 21, 2018 23:09
-
-
Save sangheestyle/0a53c5517e1fc1d5358606ea151aaffa to your computer and use it in GitHub Desktop.
Three different ways with https.get() in node.js
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
const https = require('https'); | |
https.get('https://google.com', resp => { | |
let data = ''; | |
resp.on('data', chunk => data += chunk); | |
resp.on('end', () => console.log(data)); | |
}).on('error', err => console.log(`Error: ${err}`)); |
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
const https = require('https'); | |
function getAsync(url) { | |
return new Promise((resolve, reject) => { | |
https | |
.get(url, resp => { | |
let data = ''; | |
resp.on('data', chunk => data += chunk); | |
resp.on('end', () => resolve(data)); | |
}) | |
.on('error', err => reject(err)); | |
}); | |
} | |
// due to SyntaxError: await is only valid in async function | |
(async () => { | |
try { | |
const data = await getAsync('https://google.com'); | |
console.log(data); | |
} catch (err) { | |
console.log(`err: ${err}`); | |
} | |
})(); |
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
const https = require('https'); | |
const promise = new Promise((resolve, reject) => { | |
https | |
.get('https://google.com', resp => { | |
let data = ''; | |
resp.on('data', chunk => data += chunk); | |
resp.on('end', () => resolve(data)); | |
}) | |
.on('error', err => reject(err)); | |
}); | |
promise.then(data => { | |
console.log(data); | |
}) | |
.catch(err => { | |
console.log(err); | |
}); |
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
const request = require('request'); | |
const requestPromise = (url) => new Promise((resolve, reject) => { | |
request( | |
url, | |
{ json: true}, | |
(err, res, body) => { | |
if (err) { | |
reject(err); | |
return; | |
} | |
resolve(body); | |
}); | |
}); | |
(async () => { | |
try { | |
const body = await requestPromise('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY'); | |
console.log(body); | |
} catch (err) { | |
console.log(err); | |
} | |
})(); |
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
const request = require('request'); | |
request( | |
'https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY', | |
{ json: true}, | |
(err, res, body) => { | |
if (err) { | |
return console.log(err); | |
} | |
console.log(body.url); | |
console.log(body.explanation); | |
}); |
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
const request = require('request'); | |
const requestPromise = (url) => new Promise((resolve, reject) => { | |
request( | |
url, | |
{ json: true}, | |
(err, res, body) => { | |
if (err) { | |
reject(err); | |
return; | |
} | |
resolve(body); | |
}); | |
}); | |
requestPromise('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY') | |
.then(console.log) | |
.catch(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment