Skip to content
Myr

app.js

This file

Path

examples/github/app.js

115 lines · 2.5 KiB · JavaScript

Last changed by

Updated examples to use express.<middleware>Tj Holowaychukbefore adoption90a1a2f

History of this file

Take it

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

Removed `make install-support`Tj Holowaychukbefore adoptionbdfd813

lines 1–80why, one step back
1
2/**3 * Module dependencies.4 */56var express = require('./../../lib/express'),7 http = require('http');89var app = express.createServer();1011// Expose our views1213app.set('views', __dirname + '/views');1415/**16 * Request github json api `path`.17 *18 * @param {String} path19 * @param {Function} fn20 * @api public21 */2223function request(path, fn){24 var client = http.createClient(80, 'github.com'),25 req = client.request('GET', '/api/v2/json' + path, { Host: 'github.com' });26 req.addListener('response', function(res){27 res.body = '';28 res.addListener('data', function(chunk){ res.body += chunk; });29 res.addListener('end', function(){30 try {31 fn(null, JSON.parse(res.body));32 } catch (err) {33 fn(err);34 }35 });36 });37 req.end();38}3940/**41 * Sort repositories by watchers desc.42 *43 * @param {Array} repos44 * @api public45 */4647function sort(repos){48 return repos.sort(function(a, b){49 if (a.watchers == b.watchers) return 0;50 if (a.watchers > b.watchers) return -1;51 if (a.watchers < b.watchers) return 1;52 });53}5455/**56 * Tally up total watchers.57 *58 * @param {Array} repos59 * @return {Number}60 * @api public61 */6263function totalWatchers(repos) {64 return repos.reduce(function(sum, repo){65 return sum + repo.watchers;66 }, 0);67}6869/**70 * Default to my user name :)71 */7273app.get('/', function(req, res){74 res.redirect('/repos/visionmedia');75});7677/**78 * Display repos.79 */80

Updated examplesTj Holowaychukbefore adoptionf60bcca

lines 81–82why, one step back
81app.get('/repos/*', function(req, res, next){
82 var names = req.params[0].split('/'),

Added compare view to github exampleTj Holowaychukbefore adoption1581267

lines 83–99why, one step back
83 users = [];
84 (function fetchData(name){85 // We have a user name86 if (name) {87 console.log('... fetching %s', name);88 request('/repos/show/' + name, function(err, user){89 if (err) {90 next(err)91 } else {92 user.totalWatchers = totalWatchers(user.repositories);93 user.repos = sort(user.repositories);94 user.name = name;95 users.push(user);96 fetchData(names.shift());97 }98 });99 // No more users

Removed `make install-support`Tj Holowaychukbefore adoptionbdfd813

line 100why, one step back
100 } else {

Added compare view to github exampleTj Holowaychukbefore adoption1581267

line 101why, one step back
101 console.log('... done');

Removed `make install-support`Tj Holowaychukbefore adoptionbdfd813

lines 102–103why, one step back
102 res.render('index.jade', {
103 locals: {

Added compare view to github exampleTj Holowaychukbefore adoption1581267

line 104why, one step back
104 users: users

Removed `make install-support`Tj Holowaychukbefore adoptionbdfd813

lines 105–107why, one step back
105 }
106 });107 }

Added compare view to github exampleTj Holowaychukbefore adoption1581267

line 108why, one step back
108 })(names.shift());

Removed `make install-support`Tj Holowaychukbefore adoptionbdfd813

lines 109–111why, one step back
109});
110111// Serve statics from ./public

Updated examples to use express.<middleware>Tj Holowaychukbefore adoption90a1a2f

line 112why, one step back
112app.use(express.staticProvider(__dirname + '/public'));

Removed `make install-support`Tj Holowaychukbefore adoptionbdfd813

lines 113–115why, one step back
113
114// Listen on port 3000115app.listen(3000);