Node Js Simple Weather App With Geocoding and Yargs

November 14, 2019

It's been quite a while since I left blogging activity. It was because I came back to my country and after that, I must return back to Poland to continue my second semester. Well, to be honest, it's getting harder sometimes in the beginning but Alhamdulillah, I enjoy the process now.

node js weather app, Node Js Simple Weather App, node js weather app github, node js weather app, Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github node js weather api, node js weather app github, node js weather app, node js weather app, Node Js Simple Weather App, node js weather app, Node Js Simple Weather App, node js weather app github, node js weather app, Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github node js weather api, node js weather app github, node js weather app, node js weather app, Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github node js weather app github, node js weather api, node js weather app github Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github
Node Js Simple Weather App With Geocoding and Yargs
I have already written 2 articles about How to get Weather information Using free weather API service using Nodejs and How to get information about a specific location using a free geocoding API service. Detail explanation about Free weather API can be accessed here: Node Js Simple Weather App Using Darksky API - Node Js Tutorial and Geocoding here: Node Js Geocoding to get Latitude and Longitude of Address Using Mapbox API - Node Js Tutorial.

So, as we will be using the console, to get the input value we need yargs module for Node js. Yargs module gives us the possibility to get 'advanced input' from the console comparing with argv. To learn about Yargs, you can access here Nodejs Yargs.

Here I have 3 classes
node js weather app, Node Js Simple Weather App, node js weather app github, node js weather app, Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github node js weather api, node js weather app github, node js weather app, node js weather app, Node Js Simple Weather App, node js weather app, Node Js Simple Weather App, node js weather app github, node js weather app, Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github node js weather api, node js weather app github, node js weather app, node js weather app, Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github node js weather app github, node js weather api, node js weather app github Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github
classes

App.js - Node Js Simple Weather App With Geocoding and Yargs

This is the main file that will call all the written functions from geo.js and weather.js class.


const geo = require('./geo')
const weather = require('./weather')

const yargs = require('../node_modules/yargs')

//here is the function to call the function from ohter class (geo and weather function)
const myfunc = (location) => {
//1. first we need to load the location
    geo.geo(location, (err, dataLocation) => {
//2. if the the app can retrieve the data from the API server, then we should call the weather function 
        if (err === undefined) {
//3. load the weather function to get the weather information using the longitude and latitude that have been gotten
            weather.weather(dataLocation.lat, dataLocation.long, (err, data) => {
                if (err === undefined) {
//4. print the information
                    console.log('location : ', dataLocation.location)
                    console.log(data)
                } else {
                    console.log(err)
                }
            })
        } else {
//if the location is wrong or cannot retrieve infromation
            console.log(err)
        }
    })
}
//yargs command to get input in console as value
yargs.command({
    command: 'checkweather',
    describe: 'to check the weather...',
    builder: {
        area: {
            describe: 'choose your area..',
            demandOption: true,
            type: 'string'
        }
    },
    handler: function (argv) {
        myfunc(argv.area)
    }
})

yargs.parse()

// seeIt('wroclaw') //you can uncomment this if we don't want to use Yargs module, however you should fill the location manually to the code

Geo.js - Node Js Simple Weather App With Geocoding and Yargs

This file contains all the code needed for geocode configuration.

const request = require('request')

const geo = (place, callback) => {
    const url = `https://api.mapbox.com/geocoding/v5/mapbox.places/${place}.json?limit=1&access_token=pk.eyJ1IjoiemVuaHV6YWluaSIsImEiOiJjanlzeXRobTQwMTZ3M2JwMXFsdXFlbDdsIn0.-MWjWymxxtz1_1BbMPmmRg`
    request({ url, json: true }, (error, response) => {
        if (error) {
            // return console.log('no internet')
            //to return the value then we must use callback
            callback('check your internet', undefined)
        } else if (response.body.features.length == 0) {
            //to return the value then we must use callback
            // return console.log('try other keywords')
            callback('check your keywords or try another keyword', undefined)
        } else {
            const longitude = response.body.features[0].center[0]
            const latitude = response.body.features[0].center[1]
            const exactLocation = response.body.features[0].place_name

            const data = {
                location: exactLocation,
                lat: latitude,
                long: longitude
            }

            // console.log(data)
            //to return the value then we must use callback
            callback(undefined, data)
        }
    })
}

// geo('plac grundwaldzki', (err, res) => {
//     if (err == undefined) {
//         console.log('get the result', res)
//     } else {
//         console.log(err)
//     }
// })

module.exports = { geo: geo }

weather.js - Node Js Simple Weather App With Geocoding and Yargs


This file contains all the code needed for weather configuration.

const request = require('request')

const weather = (longitude, latitude, callback) => {
    const url = `https://api.darksky.net/forecast/8593f986c0f88727f3270a097ee4b90c/${longitude},${latitude}?units=si`
    request({ url, json: true }, (err, response) => {
        if (err) {
            callback('check your internet', undefined)
        } else if (response.body.code == 400) {
            callback('the given location is invalid', undefined)
        } else {
            const data = {
                timezone: response.body.currently.timezone,
                summary: response.body.currently.summary,
                currentTemperature: response.body.currently.temperature,
                humidity: response.body.currently.humidity
            }
            callback(undefined, data)
        }
    })
}

// weather('37.8267', '-122.4233', (err, res) => {
//     if (err == undefined) {
//         console.log(res)
//     } else {
//         console.log(err)
//     }
// })

module.exports = { weather: weather }

How to run it - Node Js Simple Weather App With Geocoding and Yargs

node app.js checkweather --area="your location"
The result
node js weather app, Node Js Simple Weather App, node js weather app github, node js weather app, Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github node js weather api, node js weather app github, node js weather app, node js weather app, Node Js Simple Weather App, node js weather app, Node Js Simple Weather App, node js weather app github, node js weather app, Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github node js weather api, node js weather app github, node js weather app, node js weather app, Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github node js weather app github, node js weather api, node js weather app github Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github
result example

I also have aploaded the code on my Github. You can take a look here. Thank you

Keyword:
node js weather app, Node Js Simple Weather App, node js weather app github, node js weather app, Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github node js weather api, node js weather app github, node js weather app, node js weather app, Node Js Simple Weather App, node js weather app, Node Js Simple Weather App, node js weather app github, node js weather app, Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github node js weather api, node js weather app github, node js weather app, node js weather app, Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github node js weather app github, node js weather api, node js weather app github Node Js Simple Weather App, node js weather app github, node js weather api, node js weather app github

Share this

Related Posts

Previous
Next Post »