JavaScript Node.js Basics Create a Command Line Weather Application Retrieving Data - Solution

Peter Retvari
Peter Retvari
Full Stack JavaScript Techdegree Student 8,392 Points

My solution for open openweather.org :)

Hi folks, hope it helps for someone. Here is my version with the open weather API:

// Problem: get the weather data with numbers or city names by typing in the console and print out the results

// Solution: use the OpenWeather API key to get the data and display it

// Function to get the HTTPS module

const https = require('https');

// Get access to the HTTP module

const http = require('http');

// Query variable from the command line

let query = process.argv.slice(2).join(',').replace(' ', ',');


  // If the users types number it will change the q to id

  if (isNaN(query)===false){
    //generate an country ID query like => id= 234242
    query = `id=${query}`;
  }else {
    //generate a simple q query like => q=${city}
    query = `q=${query}`;
  }


// Function to print error messages

function printError (error){
  console.error(error.message);
};

// Function tp print message to the console

function printMessage(city, temp){
  const message = `In ${city} the current temperature is ${temp} Celsius.`;
  console.log(message);
}

// Function to convert Farenheit to celsius
// Celsius = Kelvin - 273.15

function getCelsius (kelvin){
  const celsius = kelvin - 273.15;
  return Math.round(celsius);
}

// Connect to the OpenWeather API and get the raw data
// Example of calls: api.openweathermap.org/data/2.5/weather?q=London,uk


function getTemp (query) {
    try{ //try block starts
      const request = https.get(`https://api.openweathermap.org/data/2.5/weather?${query}&APPID=8af2aa7fa978da0c3dc608a85406875c`, response => {
   if (response.statusCode === 200) {
          let body = '';
          // Read the data
          response.on('data', data => {
            body += data.toString();

          }); //data reads ends
          // Parse it 

          response.on('end', data => {
            try{  //try block to filter the undefiend cities
              const weather = JSON.parse(body);
                 printMessage(weather.name, getCelsius(weather.main.temp));

            } // udefiend city filter ends
              catch(error){
                      printError(error) // we catch the parse error an print it out
                      }
          }); // parse ends
 } // if ok status ends
      else { // if the status code is not 200
  const message = `There was an error getting your request ${query} (${http.STATUS_CODES[response.statusCode]}) `;
  const statusCodeError = new Error(message);
  printError(statusCodeError);
  }

}); //request ends  
    // URL error handling
request.on('error', () => {
    console.log('The URL is wrong. Please type another one.');
  });
} //try block ends
catch(error){
 printError(error);
}

} // getTemp funciton ends

getTemp(query); // call getTemp with the proper query

You can reach the workspace here too: https://w.trhou.se/m0vvj5e5sy

1 Answer

mersadajan
mersadajan
16,132 Points

You don't have to convert the temperature, you can add the "metric" parameter to the URL and the API will return to you the temperature in the unit you want. Your API key should also be stored in a json file. If you ever publish that code, everyone would have access to your API key(just like I have now lol). And you don't want that.

const https =  require("https");
const querystring = require("querystring");
const api = require("./api.json");


function get(city, country){
    const parameters = {
        APPID: api.key,
        units: "metric",
        query: ``
    }

    console.log(querystring.stringify(parameters));


    if(country === null || country === undefined){
        parameters.query = `${city}`;
    } else {
        parameters.query = `${city},${country}`;
    }

    const apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${querystring.stringify(parameters)}`;
    console.log(apiUrl);

    const request = https.get(apiUrl, response => {
        let body = "";

        response.on("data", chunk => {
            body += chunk;
        });

        response.on("end", () => {
            console.log(body);                  
            });
    });


}

module.exports.get = get;