-
Notifications
You must be signed in to change notification settings - Fork 0
/
forecast.js
65 lines (53 loc) · 2.26 KB
/
forecast.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
// Problem: I want the weather forcast!
// Solution: Use node js to connect to wunderground api and show the weather forecast
// Sign up for your own free wunderground developer key at https://www.wunderground.com/weather/api/
// Requires
var http = require('http');
var apiFile = require('./apiFile'); // apiKey in separate apiFile.js file to protect it :)
// function to print out the weather
function printWeather(zip, forecast) {
var message = 'Your weather forcast is: ' + forecast;
console.log(message);
};
// function to print out errors
function printError(error) {
console.error(error.message);
if (error.message === "Cannot read property 'txt_forecast' of undefined") {
console.log('Try entering a real 5-digit zip!');
};
};
// Connect to wunderground API
function getForecast(enteredZip) {
var request = http.get('http://api.wunderground.com/api/' + apiFile.apiKey + '/forecast/q/' + enteredZip + '.json', function(response) {
// console.log(response.statusCode); // for testing to see the status code
var body = ''; // start with an empty body since node gets responses in chunks
// Read the data
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
if ( response.statusCode === 200 ) {
try {
// Parse the data
var forecast = JSON.parse(body);
//console.dir(forecast); // for testing to see the JSON response
// Print the data
printWeather(enteredZip, forecast.forecast.txt_forecast.forecastday[0].fcttext);
} catch(error) {
// Print any errors
printError(error);
};
} else {
// Status code error
printError({message: 'OOPS! There was a problem getting the weather! (' + response.statusCode + ')'});
};
});
});
// Print connection error
request.on('error', printError);
// end getForecast function
};
// uncomment below if you want to run this file from the command line. ex: node forecast.js 28285
getForecast(process.argv); // process.argv lets you pass a zip in command line. ex: node forecast.js 28285
// uncomment below if you want to run this forecast from a separate file. ex: node app.js 28285
// module.exports.getForecast = getForecast;