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 trialJayaraman Thiyyadi
9,031 PointsCreate command line weather application
Hi,
I guess the weather API provider does not support "HTTPS" request. Please guide me what to do next
const https = require("https");
const http = require("http");
const key = "7229440fa6c2b9d94c1c20a3e70db01a",
url = "https://api.openweathermap.org/data/2.5/weather?";
const printError = code => {
if (typeof code == "number") {
console.error(`There is something wrong with the request. (${code}) (${http.STATUS_CODES[code]})`);
} else {
console.error(`There is something wrong with the request. (${code})`);
}
}
const printResult = data => {
process.stdout.write(data);
}
const getDetails = (value) => {
try {
https.get(url+`id=${value}&${key}`, response => {
if(response.statusCode === 200) {
let result = '';
response.on('data', resp => {
result += resp.toString();
}).on('end', () => {
try {
result = JSON.parse(result);
printResult(result);
} catch (error) {
printError(error.message);
}
});
} else {
printError(response.statusCode);
}
}).on("error", error => {
printError(error.message);
});
} catch (error) {
printError(error.message);
}
}
const username = process.argv.splice(2);
for(value of username) {
getDetails(value);
}