-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.js
39 lines (35 loc) · 1.08 KB
/
profile.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
var https = require('https');
var http = require('http');
// print message
function printMessage(username, badgeCount, points){
var message = username + " has " + badgeCount + " total badge(s) and " + points + " in JavaScript";
console.log(message);
};
//print error
function printError(error){
console.error(error.message);
}
function get(username){
var request = https.get('https://teamtreehouse.com/' + username + '.json', function(response){
var body = "";
//Read data
response.on('data', function(chunk){
body += chunk;
});
response.on('end', function(){
if(response.statusCode === 200) {
try {
var profile = JSON.parse(body);
//Print data
printMessage(username, profile.badges.length, profile.points.JavaScript);
} catch (error) {
printError(error);
}
} else {
printError({message: "There was an error getting the profile for "+ username + ". (" + http.STATUS_CODES[response.statusCode] + ")"})
}
});
});
request.on('error', printError);
}
module.exports.get = get;