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

Jasper Peijer
Jasper Peijer
18,537 Points

Solution to the Weather API

Use Dark Sky API which give you a daily 1000 free API requests. You can use the following code to request data:

app.js:

const https = require('https');
const temp = require('./temperature')

var lat = process.argv[2];
var long = process.argv[3];

function printText(summary, temperature){
console.log(`${summary}, ` + (Math.round(temp.convert(temperature) * 10) / 10)  + `°C`);
}

const request = https.get(`https://api.darksky.net/forecast/fef17a1918a4ea27dc36a500b292d1bb/${lat},${long}`, response => {
    var body = "";
    response.on('data', data => {
        body += data;
    });

    response.on('end', data => {
        var result = JSON.parse(body);
        printText(result.currently.summary, result.currently.temperature);
    });
});

temperature.js:

function convert(fahrenheit){
    return (fahrenheit - 32) * 0.5556;
}

module.exports.convert = convert;