-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi-fetch.js
More file actions
49 lines (42 loc) · 1.49 KB
/
api-fetch.js
File metadata and controls
49 lines (42 loc) · 1.49 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require('dotenv').config()
const fetch = require('node-fetch');
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
const baseURL = 'https://sandbox-api.brewerydb.com/v2/';
const favBrews = [];
const get = (zip) => {
fetch(baseURL + 'locations/?key=' + process.env.KEY + '&postalCode=' + zip)
.then(res => {
if(!res.ok) {
throw Error(res.statusText)
} return res.json()
})
.then(obj => {
const totalResults = obj.totalResults;
const data = obj.data[0];
const breweryName = data.name;
const breweryAddress = data.streetAddress;
const breweryPhone = data.phone;
const breweryWebsite = data.website;
console.log(`There are ${totalResults} results in that zip code!`);
console.log(breweryName);
console.log(breweryAddress);
console.log(breweryPhone);
console.log(breweryWebsite);
readline.question(`Save as a Favorite? `, (answer) => {
if (answer == 'yes') {
favBrews.push(breweryName)
console.log('Your favorite breweries are: ' + favBrews);
};
readline.close();
});
})
.catch(err => {
console.log(`Sorry, there are no breweries in your area.`);
})
}
readline.question(`What's your zip? `, (zip) => {
get(zip)
})