Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 0e0cc30 point at it.

DocsTj Holowaychukbefore adoption0e0cc30

lines 1–8why, one step back
1
2// Expose modules in ./support for demo purposes3require.paths.unshift(__dirname + '/../../support');45/**6 * Module dependencies.7 */8

fixed github exampleTj Holowaychukbefore adoptionf5af66a

lines 9–10why, one step back
9var express = require('./../../lib/express')
10 , http = require('http');

DocsTj Holowaychukbefore adoption0e0cc30

lines 11–16why, one step back
11
12var app = express.createServer();1314// Expose our views1516app.set('views', __dirname + '/views');

fixed github exampleTj Holowaychukbefore adoptionf5af66a

line 17why, one step back
17app.set('view engine', 'jade');

DocsTj Holowaychukbefore adoption0e0cc30

lines 18–27why, one step back
18
19/**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 back
28 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 }

DocsTj Holowaychukbefore adoption0e0cc30

line 39why, one step back
39 });

fixed github exampleTj Holowaychukbefore adoptionf5af66a

lines 40–41why, one step back
40 });
41 req.end();

DocsTj Holowaychukbefore adoption0e0cc30

lines 42–51why, one step back
42}
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 back
52 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 });

DocsTj Holowaychukbefore adoption0e0cc30

lines 57–67why, one step back
57}
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 back
68 return repos.reduce(function(sum, repo){
69 return sum + repo.watchers;70 }, 0);

DocsTj Holowaychukbefore adoption0e0cc30

lines 71–77why, one step back
71}
7273/**74 * Default to my user name :)75 */7677app.get('/', function(req, res){

fixed github exampleTj Holowaychukbefore adoptionf5af66a

line 78why, one step back
78 res.redirect('/repos/visionmedia');

DocsTj Holowaychukbefore adoption0e0cc30

lines 79–85why, one step back
79});
8081/**82 * Display repos.83 */8485app.get('/repos/*', function(req, res, next){

fixed github exampleTj Holowaychukbefore adoptionf5af66a

lines 86–94why, one step back
86 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)

DocsTj Holowaychukbefore adoption0e0cc30

line 95why, one step back
95 } else {

fixed github exampleTj Holowaychukbefore adoptionf5af66a

lines 96–100why, one step back
96 user.totalWatchers = totalWatchers(user.repositories);
97 user.repos = sort(user.repositories);98 user.name = name;99 users.push(user);100 fetchData(names.shift());

DocsTj Holowaychukbefore adoption0e0cc30

line 101why, one step back
101 }

fixed github exampleTj Holowaychukbefore adoptionf5af66a

lines 102–108why, one step back
102 });
103 // No more users104 } else {105 console.log('... done');106 res.render('index', { users: users });107 }108 })(names.shift());

DocsTj Holowaychukbefore adoption0e0cc30

lines 109–116why, one step back
109});
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');