This repository was archived by the owner on Mar 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-promise.js
More file actions
52 lines (44 loc) · 1.37 KB
/
app-promise.js
File metadata and controls
52 lines (44 loc) · 1.37 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
50
51
52
const _ = require('lodash');
const axios = require('axios');
const dotenv = require('dotenv');
dotenv.config();
const yargs = require('yargs');
const argv = yargs
.options({
a: {
demand: true,
alias: 'address',
describe: 'Address to fetch weather for.',
string: true,
},
})
.help()
.alias('help', 'h').argv;
const encodedAddress = encodeURIComponent(argv.address);
const geocodeUrl = `https://api.mapbox.com/geocoding/v5/mapbox.places/${encodedAddress}.json?access_token=${process.env.MAPBOX_API}`;
axios
.get(geocodeUrl)
.then((response) => {
if (response.data.features[0] === undefined) {
throw new Error('Unable to find that address.');
}
const lat = response.data.features[0].geometry.coordinates[1];
const lng = response.data.features[0].geometry.coordinates[0];
const weatherUrl = `https://api.darksky.net/forecast/${process.env.DARKSKY_SECRET_KEY}/${lat},${lng}`;
console.log(response.data.features[0].place_name);
return axios.get(weatherUrl);
})
.then((response) => {
const temperature = response.data.currently.temperature;
const feelsLike = response.data.currently.apparentTemperature;
console.log(
`It's currently ${temperature}. It feels like ${feelsLike}.`
);
})
.catch((error) => {
if (error.code === 'ENOTFOUND') {
console.log('Unable to connect to API servers.');
} else {
console.log(error.message);
}
});