4.10 Wiring Up Weather Search

Goto https://darksky.net/dev to register for a developer account to use forecast.io API (Get the key)

const request = require('request');
request({
url: "https://api.darksky.net/forecast/a4fdda5fd0d59408f5addb4bd022a199/37.09024,-95.712891",
json: true
}, (err, res, body)=> {
console.log(body.currently.temperature);
})

Adding Error Handling

request({
url: "https://api.darksky.net/forecast/a4fdda5fd0d59408f5addb4bd022a199/37.09024,-95.712891",
json: true
}, (err, res, body)=> {
if (err) {
console.log("unable to connect to Forecast.io Server.");
} else if (body.code === 400) {
console.log(body.error);
} else {
console.log(body.currently.temperature);
}
})