app.js
This file
Path
examples/github/app.js
116 lines · 2.4 KiB · JavaScript
Last changed by
fixed github exampleTj Holowaychukbefore adoptionf5af66a
History of this fileTake it
The walk stopped after 200 commits: lines older than 5152823 point at it.
Regenerated docsTj Holowaychukbefore adoption5152823
lines 1–8why, one step back12// Expose modules in ./support for demo purposes3require.paths.unshift(__dirname + '/../../support');45/**6 * Module dependencies.7 */8fixed github exampleTj Holowaychukbefore adoptionf5af66a
lines 9–10why, one step back9var express = require('./../../lib/express')10 , http = require('http');Regenerated docsTj Holowaychukbefore adoption5152823
lines 11–16why, one step back1112var app = express.createServer();1314// Expose our views1516app.set('views', __dirname + '/views');fixed github exampleTj Holowaychukbefore adoptionf5af66a
line 17why, one step back17app.set('view engine', 'jade');Regenerated docsTj Holowaychukbefore adoption5152823
lines 18–27why, one step back1819/**20 * Request github json api `path`.21 *22 * @param {String} path23 * @param {Function} fn24 * @api public25 */2627function request(path, fn){fixed github exampleTj Holowaychukbefore adoptionf5af66a
lines 28–38why, one step back28 var client = http.createClient(80, 'github.com')29 , req = client.request('GET', '/api/v2/json' + path, { Host: 'github.com' });30 req.addListener('response', function(res){31 res.body = '';32 res.addListener('data', function(chunk){ res.body += chunk; });33 res.addListener('end', function(){34 try {35 fn(null, JSON.parse(res.body));36 } catch (err) {37 fn(err);38 }Regenerated docsTj Holowaychukbefore adoption5152823
line 39why, one step back39 });fixed github exampleTj Holowaychukbefore adoptionf5af66a
lines 40–41why, one step back40 });41 req.end();Regenerated docsTj Holowaychukbefore adoption5152823
lines 42–51why, one step back42}4344/**45 * Sort repositories by watchers desc.46 *47 * @param {Array} repos48 * @api public49 */5051function sort(repos){fixed github exampleTj Holowaychukbefore adoptionf5af66a
lines 52–56why, one step back52 return repos.sort(function(a, b){53 if (a.watchers == b.watchers) return 0;54 if (a.watchers > b.watchers) return -1;55 if (a.watchers < b.watchers) return 1;56 });Regenerated docsTj Holowaychukbefore adoption5152823
lines 57–67why, one step back57}5859/**60 * Tally up total watchers.61 *62 * @param {Array} repos63 * @return {Number}64 * @api public65 */6667function totalWatchers(repos) {fixed github exampleTj Holowaychukbefore adoptionf5af66a
lines 68–70why, one step back68 return repos.reduce(function(sum, repo){69 return sum + repo.watchers;70 }, 0);Regenerated docsTj Holowaychukbefore adoption5152823
lines 71–77why, one step back71}7273/**74 * Default to my user name :)75 */7677app.get('/', function(req, res){fixed github exampleTj Holowaychukbefore adoptionf5af66a
line 78why, one step back78 res.redirect('/repos/visionmedia');Regenerated docsTj Holowaychukbefore adoption5152823
lines 79–85why, one step back79});8081/**82 * Display repos.83 */8485app.get('/repos/*', function(req, res, next){fixed github exampleTj Holowaychukbefore adoptionf5af66a
lines 86–94why, one step back86 var names = req.params[0].split('/')87 , users = [];88 (function fetchData(name){89 // We have a user name90 if (name) {91 console.log('... fetching \x1b[33m%s\x1b[0m', name);92 request('/repos/show/' + name, function(err, user){93 if (err) {94 next(err)Regenerated docsTj Holowaychukbefore adoption5152823
line 95why, one step back95 } else {fixed github exampleTj Holowaychukbefore adoptionf5af66a
lines 96–100why, one step back96 user.totalWatchers = totalWatchers(user.repositories);97 user.repos = sort(user.repositories);98 user.name = name;99 users.push(user);100 fetchData(names.shift());Regenerated docsTj Holowaychukbefore adoption5152823
line 101why, one step back101 }fixed github exampleTj Holowaychukbefore adoptionf5af66a
lines 102–108why, one step back102 });103 // No more users104 } else {105 console.log('... done');106 res.render('index', { users: users });107 }108 })(names.shift());Regenerated docsTj Holowaychukbefore adoption5152823
lines 109–116why, one step back109});110111// Serve statics from ./public112app.use(express.staticProvider(__dirname + '/public'));113114// Listen on port 3000115app.listen(3000);116console.log('Express app started on port 3000');