Skip to content
Myr

removed old github example

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent cc84f40 · Browse files · 5 files+0 added−159 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+0 added−159 removed

  • other:+0 added−159 removed

5 files+0 added−159 removed

deleted:examples/github/public/style.css-19 removed
deleted:examples/github/views/index.jade-14 removed
12extends layout34block content5 each user in users6 .user7 h2= user.name8 p.summary9 | <a href="http://github.com/#{user.name}">#{user.name}</a> has10 | <strong>#{user.repos.length}</strong> repositories 11 | with a total of <strong>#{user.totalWatchers}</strong> watchers.12 table#repos13 for repo in user.repos14 include repo
deleted:examples/github/views/layout.jade-8 removed
1!!!2html3 head4 title Github Example5 link(rel="stylesheet", href="/style.css")6 body7 #container8 block content
deleted:examples/github/views/repo.jade-5 removed
1tr.repo2 td.name3 a(href: repo.homepage || repo.url)= repo.name4 td.watchers5 = repo.watchers
deleted:examples/github/app.js-113 removed
12/**3 * Module dependencies.4 */56var express = require('../../lib/express')7 , http = require('http');89var app = express();1011// Expose our views1213app.set('views', __dirname + '/views');14app.set('view engine', 'jade');1516/**17 * Request github json api `path`.18 *19 * @param {String} path20 * @param {Function} fn21 * @api public22 */2324function request(path, fn){25 var client = http.createClient(80, 'github.com')26 , req = client.request('GET', '/api/v2/json' + path, { Host: 'github.com' });27 req.on('response', function(res){28 res.body = '';29 res.on('data', function(chunk){ res.body += chunk; });30 res.on('end', function(){31 try {32 fn(null, JSON.parse(res.body));33 } catch (err) {34 fn(err);35 }36 });37 });38 req.end();39}4041/**42 * Sort repositories by watchers desc.43 *44 * @param {Array} repos45 * @api public46 */4748function sort(repos){49 return repos.sort(function(a, b){50 if (a.watchers == b.watchers) return 0;51 if (a.watchers > b.watchers) return -1;52 if (a.watchers < b.watchers) return 1;53 });54}5556/**57 * Tally up total watchers.58 *59 * @param {Array} repos60 * @return {Number}61 * @api public62 */6364function totalWatchers(repos) {65 return repos.reduce(function(sum, repo){66 return sum + repo.watchers;67 }, 0);68}6970/**71 * Default to my user name :)72 */7374app.get('/', function(req, res){75 res.redirect('/repos/visionmedia');76});7778/**79 * Display repos.80 */8182app.get('/repos/*', function(req, res, next){83 var names = req.params[0].split('/')84 , users = [];85 (function fetchData(name){86 // We have a user name87 if (name) {88 console.log('... fetching \x1b[33m%s\x1b[0m', name);89 request('/repos/show/' + name, function(err, user){90 if (err) {91 next(err)92 } else {93 user.totalWatchers = totalWatchers(user.repositories);94 user.repos = sort(user.repositories);95 user.name = name;96 users.push(user);97 fetchData(names.shift());98 }99 });100 // No more users101 } else {102 console.log('... done');103 res.render('index', { users: users });104 }105 })(names.shift());106});107108// Serve statics from ./public109app.use(express.static(__dirname + '/public'));110111// Listen on port 3000112app.listen(3000);113console.log('Express app started on port 3000');