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 Basics 2017 Create a Command Line Weather Application Retrieving Data - Solution

Jasper Peijer
Jasper Peijer
45,009 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;