-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
46 lines (37 loc) · 1.04 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//connect to api url https://teamtreehouse.com/username
//read data
//prints message to console
const https=require('https');
function printErr(error){
console.error(error.message);
}
function printMessage(username,badgecount,points){
const message=`${username} has ${badgecount} total badges and ${points} points in javascript`;
console.log(message);
}
//connect to api
function getProfile(username){
try{
const request = https.get(`https://teamtreehouse.com/${username}.json`,response=>{
let body = "";
response.on('data',data=>{
body+=data.toString();
});
response.on('end',data=>{
try{
const profile=JSON.parse(body);
printMessage(username,profile.badges.length,profile.points.JavaScript);
} catch(error){
console.error(error.message);
}
});
});
request.on('error',error=>console.error(`problem with request:${error.message}`));
}catch(error){
console.error(error.message);
}
}
const users=process.argv.slice(2);
users.forEach(username=>{
getProfile(username);
});