-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
26 lines (23 loc) · 668 Bytes
/
index.js
File metadata and controls
26 lines (23 loc) · 668 Bytes
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
const fs = require("fs");
const axios = require("axios");
const apiURL = "https://data.ct.gov/resource/y6p2-px98.json?category=Fruit&item=Peaches";
const getData = async (url) => {
try {
// data will be in response.data so we can destructure using -> {data} = await axios.get()
let { data } = await axios.get(url);
return data;
} catch (error) {
console.error(error);
}
};
getData(apiURL)
.then((data) =>
fs.writeFile("./data_from_api.json", JSON.stringify(data, null, 4), (err) => {
if (err) {
console.error(err);
return;
}
console.log("File created");
})
)
.catch((err) => console.error(err));