fixed github example
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 540192f · Browse files · 4 files·+52 added−55 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+52 added−55 removed
- other:+52 added−55 removed
Provenance
- Commit
f5af66a- Tree
ee8675c- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
540192f
4 files changed+52 added−55 removed
modified:examples/github/public/style.css+1 added-1 removed
10 unmodified lines1112131415151617181910 unmodified lines}.user { margin: 0 10px; float: left; width: 25%; width: 20%;}table td:nth-child(2) { padding: 0 5px;}No newline at end of fileNo newline at end of filedeleted:examples/github/views/partials/repo.jade-5 removed
1-tr.repo2- td.name3- a(href: repo.homepage || repo.url)= repo.name4- td.watchers5- = repo.watchersadded:examples/github/views/_repo.jade+5 added
1+tr.repo2+ td.name3+ a(href: repo.homepage || repo.url)= repo.name4+ td.watchers5+ = repo.watchersmodified:examples/github/app.js+46 added-49 removed
4 unmodified lines567891091011121314151617181920212 unmodified lines24252627272829303132333435363738282930313233343536373839404041424344452 unmodified lines4849505151525354555253545556575859603 unmodified lines646566676768696869707172737475767777787980818283848585868788899091929394959697989910010110286878889909192939495104105106107108109969798991001011021031041051061071111081091101111124 unmodified lines4 unmodified lines/** * Module dependencies. */
var express = require('./../../lib/express'), http = require('http');var express = require('./../../lib/express') , http = require('http');
var app = express.createServer();
// Expose our views
app.set('views', __dirname + '/views');app.set('view engine', 'jade');
/** * Request github json api `path`. *2 unmodified lines * @api public */
function request(path, fn){ var client = http.createClient(80, 'github.com'), req = client.request('GET', '/api/v2/json' + path, { Host: 'github.com' }); req.addListener('response', function(res){ res.body = ''; res.addListener('data', function(chunk){ res.body += chunk; }); res.addListener('end', function(){ try { fn(null, JSON.parse(res.body)); } catch (err) { fn(err); } }); var client = http.createClient(80, 'github.com') , req = client.request('GET', '/api/v2/json' + path, { Host: 'github.com' }); req.addListener('response', function(res){ res.body = ''; res.addListener('data', function(chunk){ res.body += chunk; }); res.addListener('end', function(){ try { fn(null, JSON.parse(res.body)); } catch (err) { fn(err); } }); }); req.end(); req.end();}
/** * Sort repositories by watchers desc.2 unmodified lines * @api public */
function sort(repos){ return repos.sort(function(a, b){ if (a.watchers == b.watchers) return 0; if (a.watchers > b.watchers) return -1; if (a.watchers < b.watchers) return 1; }); return repos.sort(function(a, b){ if (a.watchers == b.watchers) return 0; if (a.watchers > b.watchers) return -1; if (a.watchers < b.watchers) return 1; });}
/** * Tally up total watchers.3 unmodified lines * @api public */
function totalWatchers(repos) { return repos.reduce(function(sum, repo){ return sum + repo.watchers; }, 0); return repos.reduce(function(sum, repo){ return sum + repo.watchers; }, 0);}
/** * Default to my user name :) */
app.get('/', function(req, res){ res.redirect('/repos/visionmedia'); res.redirect('/repos/visionmedia');});
/** * Display repos. */
app.get('/repos/*', function(req, res, next){ var names = req.params[0].split('/'), users = []; (function fetchData(name){ // We have a user name if (name) { console.log('... fetching \x1b[33m%s\x1b[0m', name); request('/repos/show/' + name, function(err, user){ if (err) { next(err) } else { user.totalWatchers = totalWatchers(user.repositories); user.repos = sort(user.repositories); user.name = name; users.push(user); fetchData(names.shift()); } }); // No more users var names = req.params[0].split('/') , users = []; (function fetchData(name){ // We have a user name if (name) { console.log('... fetching \x1b[33m%s\x1b[0m', name); request('/repos/show/' + name, function(err, user){ if (err) { next(err) } else { console.log('... done'); res.render('index.jade', { locals: { users: users } }); user.totalWatchers = totalWatchers(user.repositories); user.repos = sort(user.repositories); user.name = name; users.push(user); fetchData(names.shift()); } }); // No more users } else { console.log('... done'); res.render('index', { users: users }); } })(names.shift()); })(names.shift());});
// Serve statics from ./publicapp.use(express.staticProvider(__dirname + '/public'));4 unmodified lines