app.js
This file
Path
examples/github/app.js
116 lines · 2.4 KiB · JavaScript
Last changed by
fixing more examplesTj Holowaychukbefore adoption66a6214
History of this fileTake it
The walk stopped after 200 commits: lines older than ce2a18f point at it.
master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
lines 1–8why, one step back12// Expose modules in ./support for demo purposes3require.paths.unshift(__dirname + '/../../support');45/**6 * Module dependencies.7 */8fixing more examplesTj Holowaychukbefore adoption66a6214
line 9why, one step back9var express = require('../../lib/express')fixed github exampleTj Holowaychukbefore adoptionf5af66a
line 10why, one step back10 , http = require('http');master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
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');master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
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–29why, one step back28 var client = http.createClient(80, 'github.com')29 , req = client.request('GET', '/api/v2/json' + path, { Host: 'github.com' });fixing more examplesTj Holowaychukbefore adoption66a6214
line 30why, one step back30 req.on('response', function(res){fixed github exampleTj Holowaychukbefore adoptionf5af66a
line 31why, one step back31 res.body = '';fixing more examplesTj Holowaychukbefore adoption66a6214
lines 32–33why, one step back32 res.on('data', function(chunk){ res.body += chunk; });33 res.on('end', function(){fixed github exampleTj Holowaychukbefore adoptionf5af66a
lines 34–38why, one step back34 try {35 fn(null, JSON.parse(res.body));36 } catch (err) {37 fn(err);38 }master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 39why, one step back39 });fixed github exampleTj Holowaychukbefore adoptionf5af66a
lines 40–41why, one step back40 });41 req.end();master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
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 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
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);master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
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');master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
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)master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
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());master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
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());master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
lines 109–111why, one step back109});110111// Serve statics from ./publicfixing more examplesTj Holowaychukbefore adoption66a6214
line 112why, one step back112app.use(express.static(__dirname + '/public'));master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
lines 113–116why, one step back113114// Listen on port 3000115app.listen(3000);116console.log('Express app started on port 3000');