Skip to content

Instantly share code, notes, and snippets.

@Shafran123
Created December 28, 2019 13:32
Show Gist options
  • Save Shafran123/f0c750f2ada9d66b7ec0597f0ac5244d to your computer and use it in GitHub Desktop.
Save Shafran123/f0c750f2ada9d66b7ec0597f0ac5244d to your computer and use it in GitHub Desktop.
Simple Weather App Using Node Js
const request = require('request')
const geoCode = require('./utils/geocode')
const forecast = require('./utils/forecast')
const address = process.argv[2]
if(!address){
console.log('Please Provide Address')
}else{
geoCode(address , (error , data)=>{
if(error){
return console.log(error)
}
forecast(data.latitude, data.longtitude , (error, forecastData) => {
if(error){
return console.log(error)
}
console.log(data.location)
console.log(forecastData)
})
})
}
const request = require('request')
const forecast = (lat ,long , callback) => {
const url = 'https://api.darksky.net/forecast/<KEY>/' + encodeURIComponent(lat) + ','+encodeURIComponent(long)+''
request({url : url, json: true} , (error , response) => {
if(error){
callback('Unable to connect location services' , undefined)
} else if(response.body.error){
callback('Unable to find location' , undefined)
}else{
callback(undefined ,
response.body.daily.data[0].summary
)
}
})
}
module.exports = forecast
const request = require('request')
const geoCode = (address , callback) => {
const url = 'https://api.mapbox.com/geocoding/v5/mapbox.places/' + encodeURIComponent(address) + '.json?access_token=<KEY>&limit=1'
request({url : url, json: true} , (error , response) => {
if(error){
callback('Unable to connect location services' , undefined)
} else if(response.body.features.length ===0 ){
callback('Unable to find location' , undefined)
}else{
callback(undefined , {
latitude : response.body.features[0].center[1],
longtitude : response.body.features[0].center[0],
location : response.body.features[0].place_name
})
}
})
}
module.exports = geoCode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment