forked from Swap76/Learn-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnasaimgHttps.js
More file actions
31 lines (30 loc) · 1.07 KB
/
nasaimgHttps.js
File metadata and controls
31 lines (30 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const https = require("https"); // requiring https module
const fs = require("fs"); // requiring fs module
const Stream = require("stream").Transform;
const options = {
hostname: "api.nasa.gov", // nasa api
port: 443, // tcp port
path: "/planetary/apod?api_key=DEMO_KEY&date=2019-08-08", // path from which image is extracted
method: "GET"
};
let wholedata = "";
const request = https.request(options, function (res) {
res.on("data", function (chunk) {
wholedata += chunk; // we get data in chunks or packets so we are adding it to get wholedata
});
res.on("end", function () {
const JSONobj = JSON.parse(wholedata); // parsing data into object form
const path = JSONobj.url;
const img = https.request(path, function (res) {
const data = new Stream(); // creating new stream
res.on("data", function (chunk) {
data.push(chunk);
});
res.on("end", function () {
fs.writeFileSync("./resources/img/file.jpg", data.read()); // saving image in file.jpg file
});
});
img.end();
});
});
request.end(); // ending request