Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Node Js Extra Credit

Hi guys.

Just wondering what you think of my Weather app for the Node Js basics course. It retrieves an address from Google geolocation API, converts it to latitude and longitude, then retrieves the current weather for that location.

I know it's a lot to ask to go over my work but this is my first difficult project and any comments/criticisms are more than welcome.

Thanks in advance

Happy coding

Paul

(function () {
    "use strict";
    const https = require("https");
    //Http included for error handling STATUS_CODES
    const http = require("http");
    const totalData = [];
    //Your API keys here
    const weatherApiKey = "get your own!!!";
    const googleApiKey = "get your own!!!";

    //Displays total data in console
    function printMessage() {
        const message = `The weather in ${totalData[0]} is ${totalData[1]}`;
        console.log(message);
    }
    //Error function
    function printError(error) {
        console.error(error.message);
    }
    // receives lat and long from google api, retrieves weather from forecast.io
    function getWeather(lat, long) {
        try {
            const weather = `https://api.darksky.net/forecast/${weatherApiKey}/${lat}, ${long}?units=si`;
            const request = https.get(weather, function (res) {
                if (res.statusCode === 200) {
                    let body = "";
                    res.on("data", function (data) {
                        body += data.toString();
                    });
                    res.on("end", function () {
                        try {
                            const weatherData = JSON.parse(body);
                            //Pushes result to totalData
                            totalData.push(`${weatherData.currently.temperature}C, ${weatherData.currently.summary}`);
                            //Calls printmessage
                            printMessage();
                        } catch (error) {
                            printError(error);
                        }
                    });
                } else {
                    const message = `There was an error getting the weather for your chosen location: Lat: ${lat}, Long: ${long}, Error ${res.statusCode} (${http.STATUS_CODES[res.statusCode]})`;
                    const statusCodeError = new Error(message);
                    printError(statusCodeError);
                }
            });
            request.on("error", function (error) {
                printError(error);
            });
        } catch (error) {
            printError(error);
        }
    }
    /* Converts address into lat and long.
    Lat and long thrown to getWeather */
    function convertLocation(address) {
        try {
            const location = `https://maps.googleapis.com/maps/api/geocode/json?address=${address}&key=${googleApiKey}`;
            const request = https.get(location, function (res) {
                if (res.statusCode === 200) {
                    let body = "";
                    res.on("data", function (data) {
                        body += data.toString();
                    });
                    res.on("end", function () {
                        try {
                            const locationData = JSON.parse(body);
                            //Retrieves location data from google api, sets lat and long
                            const locationAddress = locationData.results[0].formatted_address;
                            const lat = locationData.results[0].geometry.bounds.northeast.lat;
                            const lng = locationData.results[0].geometry.bounds.northeast.lng;
                            getWeather(lat, lng);
                            //Pushes result to totalData
                            totalData.push(locationAddress);
                        } catch (error) {
                            const message = `There was an error getting the weather for ${address}, invalid address.`;
                            const statusCodeError = new Error(message);
                            printError(statusCodeError);
                        }
                    });
                } else {
                    const message = `There was an error getting the weather for ${address}, Error ${res.statusCode} (${http.STATUS_CODES[res.statusCode]})`;
                    const statusCodeError = new Error(message);
                    printError(statusCodeError);
                }
            });
            request.on("error", function (error) {
                printError(error);
            });
        } catch (error) {
            printError(error);
        }
    }
    /* ***Enter location here***
    Calls the function */
    convertLocation("stockport");
}());