UNPKG

lesson9

Version:

just_a_test

48 lines (34 loc) 1.71 kB
var https = require("https"); //var username = 'OllieParsley'; function getRepos(username, callback){ var options ={ host:'api.github.com', path:'/users/' + username + '/repos', method:'GET' }; var request = https.request(options, function(response) { // in questo modo facciamo la chiamata (request) con le opzioni creati precedentemente var body = ''; response.on("data",function(chunk){ // questa sta ad indicare che mettiamo in ascolto il server sui dati ("data") ad ogni chunk // function (chunk) { } Emitted when a piece of the message body is received. // Ossia viene eseguita ogni volta che si riceve un pezzo di body body+=chunk.toString('utf8'); // ogni volta appendiamo il chunk al body }); response.on("end", function(){ //console.log("Body: ", body); // stampiano il body var repos = []; // creiamo un array vuoto var json = JSON.parse(body); json.forEach(function(repo){ // come per il php quando si scorre un risultato di query o un array associativo. // il nome "repo" � in nome della variabile che viene assegnata per ogni "row" repos.push({ // cosi invece infiliamo nell array vuoto creato in precedenza in piccolo oggetto fatto di name: repo.name, //nome del row .name description: repo.description //nome del row .description }); }); //console.log("Count: ", json.length); // stampiano il la lunghezza del body parsato. //console.log("Repos: ", repos); // stampiano il la lunghezza del body parsato. callback(repos); }); }); request.end(); } module.exports.getRepos = getRepos;