Created
August 11, 2019 07:40
-
-
Save rintoandrews90/ff7a1dc49a818fa639238bce29f3129c to your computer and use it in GitHub Desktop.
Short Hand Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Install HTTP Request | |
// npm init -y | |
// npm i request | |
const request = require('request') | |
const wheather = (latitude,longitude,callback) => { | |
const url = 'https://api.darksky.net/forecast/fb190dafa7914f0bb56e65287631fad9/'+latitude+','+longitude | |
request({url,json:true}, (error,response) => { | |
if (error) { | |
callback('Unable to connect to wheather API',undefined) | |
} else if (response.body.error){ | |
callback('Error to connect to wheather API',undefined) | |
} else { | |
callback(undefined,{temperature:response.body.currently.temperature,rainprobability:response.body.currently.precipProbability }) | |
} | |
}) | |
} | |
const geoCode = (address,callback) => { | |
const url = 'https://api.mapbox.com/geocoding/v5/mapbox.places/'+ encodeURIComponent(address) + '.json?access_token=pk.eyJ1IjoicmludG8iLCJhIjoiY2p6NmllczBiMG1nbjNucG5qZjVqOXZwZSJ9.qu2LrgatHMB-so2E4oHaFA&limit=1' | |
request({url,json:true}, (error,response) => { | |
if (error) { | |
callback('Unable to connect to wheather API',undefined) | |
} else if (response.body.features.length == 0){ | |
callback('Unable to find location, try another location',undefined) | |
} else { | |
const latitude = response.body.features[0].center[1] | |
const longitude = response.body.features[0].center[0] | |
callback(undefined,{latitude:latitude,longitude:longitude}) | |
} | |
}) | |
} | |
geoCode("Kerala", (error,{latitude,longitude}) => { | |
if (error) { | |
return console.log('Err',error) | |
} else { | |
console.log(latitude+ ' ' + longitude) | |
wheather(latitude,longitude,(error,forcastdata) => { | |
if(error) { | |
return console.log('Err',error) | |
} | |
console.log('Data',forcastdata) | |
}) | |
} | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment