app.js
This file
Path
examples/github/app.js
119 lines · 2.7 KiB · JavaScript
Last changed by
Exposing ./support libs to examplesTj Holowaychukbefore adoption9b7470d
History of this fileTake it
The walk stopped after 200 commits: lines older than 7dff9dd point at it.
Merge branch 'flash-example'Tj Holowaychukbefore adoption7dff9dd
line 1why, one step back1Exposing ./support libs to examplesTj Holowaychukbefore adoption9b7470d
lines 2–4why, one step back2// Expose modules in ./support for demo purposes3require.paths.unshift(__dirname + '/../../support');4Merge branch 'flash-example'Tj Holowaychukbefore adoption7dff9dd
lines 5–89why, one step back5/**6 * Module dependencies.7 */89var express = require('./../../lib/express'),10 http = require('http');1112var app = express.createServer();1314// Expose our views1516app.set('views', __dirname + '/views');1718/**19 * Request github json api `path`.20 *21 * @param {String} path22 * @param {Function} fn23 * @api public24 */2526function request(path, fn){27 var client = http.createClient(80, 'github.com'),28 req = client.request('GET', '/api/v2/json' + path, { Host: 'github.com' });29 req.addListener('response', function(res){30 res.body = '';31 res.addListener('data', function(chunk){ res.body += chunk; });32 res.addListener('end', function(){33 try {34 fn(null, JSON.parse(res.body));35 } catch (err) {36 fn(err);37 }38 });39 });40 req.end();41}4243/**44 * Sort repositories by watchers desc.45 *46 * @param {Array} repos47 * @api public48 */4950function sort(repos){51 return repos.sort(function(a, b){52 if (a.watchers == b.watchers) return 0;53 if (a.watchers > b.watchers) return -1;54 if (a.watchers < b.watchers) return 1;55 });56}5758/**59 * Tally up total watchers.60 *61 * @param {Array} repos62 * @return {Number}63 * @api public64 */6566function totalWatchers(repos) {67 return repos.reduce(function(sum, repo){68 return sum + repo.watchers;69 }, 0);70}7172/**73 * Default to my user name :)74 */7576app.get('/', function(req, res){77 res.redirect('/repos/visionmedia');78});7980/**81 * Display repos.82 */8384app.get('/repos/*', function(req, res, next){85 var names = req.params[0].split('/'),86 users = [];87 (function fetchData(name){88 // We have a user name89 if (name) {Exposing ./support libs to examplesTj Holowaychukbefore adoption9b7470d
line 90why, one step back90 console.log('... fetching \x1b[33m%s\x1b[0m', name);Merge branch 'flash-example'Tj Holowaychukbefore adoption7dff9dd
lines 91–118why, one step back91 request('/repos/show/' + name, function(err, user){92 if (err) {93 next(err)94 } else {95 user.totalWatchers = totalWatchers(user.repositories);96 user.repos = sort(user.repositories);97 user.name = name;98 users.push(user);99 fetchData(names.shift());100 }101 });102 // No more users103 } else {104 console.log('... done');105 res.render('index.jade', {106 locals: {107 users: users108 }109 });110 }111 })(names.shift());112});113114// Serve statics from ./public115app.use(express.staticProvider(__dirname + '/public'));116117// Listen on port 3000118app.listen(3000);Exposing ./support libs to examplesTj Holowaychukbefore adoption9b7470d
line 119why, one step back119console.log('Express app started on port 3000');