· 5 years ago · Feb 07, 2021, 05:56 AM
1#!/usr/bin/env node
2
3const fetch = require('node-fetch');
4const prompts = require('prompts');
5const figlet = require('figlet');
6
7const api = {
8
9 key: '31d1dda7f3c7d365a188b9408bf2b8e2',
10 base: "https://api.openweathermap.org/data/2.5/"
11
12};
13
14figlet('WeatherLi', function(err, data) {
15
16 if (err) {
17 console.log('Sorry Something Went Wrong');
18 console.dir(err);
19 return;
20 }
21
22 console.log(data);
23
24});
25
26(async () => {
27
28 const response = await prompts({
29
30 type: 'text',
31 name: 'location',
32 message: 'Enter City or Country'
33
34 });
35
36 function outputThrower(location) {
37
38 let weatherRecieved = weatherEngine(location);
39 return weatherRecieved
40
41 };
42
43 outputThrower(response.location);
44
45})();
46
47function weatherEngine(location) {
48
49 fetch(`${api.base}weather?q=${location}&units=metric&APPID=${api.key}`)
50 .then(response => response.json())
51 .then(result => {
52 console.log(result)
53 })
54 .catch(error => console.log(error));
55
56};
57