Merge branch 'github' into connect
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 · parents 38c453b, 247a947 · Browse files · 5 files·+136 added−0 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+136 added−0 removed
- other:+136 added−0 removed
Provenance
- Commit
59ac3d1- Tree
d4241c6- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
38c453b- Parent 2
247a947
5 files changed+136 added−0 removed
added:examples/github/public/style.css+11 added
1234567891011body { padding: 30px 50px; font: 12px/1.4 "Lucida Grande", "Helvetica Nueue", Arial, sans-serif;}a { color: #00AAFF; text-decoration: none;}a:hover { text-decoration: underline;}added:examples/github/views/partials/repo.jade+5 added
1+tr.repo2+ td.name3+ a(href: repo.homepage || repo.url)= repo.name4+ td.watchers5+ = repo.watchersadded:examples/github/views/index.jade+5 added
1+h1= name2+p.summary3+ | #{name} has <strong>#{repos.length}</strong> repositories 4+ | with a total of <strong>#{totalWatchers}</strong> watchers.5+table#repos!= partial('repo', repos)added:examples/github/views/layout.jade+7 added
1+!!!2+html3+ head4+ title Github Example5+ link(rel="stylesheet", href="/style.css")6+ body7+ #container!= bodyadded:examples/github/app.js+108 added
1+2+/**3+ * Module dependencies.4+ */5+6+var express = require('./../../lib/express'),7+ connect = require('connect'),8+ http = require('http');9+10+// Setup server with redirect middleware,11+// providing us with res.redirect()12+13+var app = express.createServer(14+ connect.redirect()15+);16+17+// Expose our views18+19+app.set('views', __dirname + '/views');20+21+/**22+ * Request github json api `path`.23+ *24+ * @param {String} path25+ * @param {Function} fn26+ * @api public27+ */28+29+function request(path, fn){30+ var client = http.createClient(80, 'github.com'),31+ req = client.request('GET', '/api/v2/json' + path, { Host: 'github.com' });32+ req.addListener('response', function(res){33+ res.body = '';34+ res.addListener('data', function(chunk){ res.body += chunk; });35+ res.addListener('end', function(){36+ try {37+ fn(null, JSON.parse(res.body));38+ } catch (err) {39+ fn(err);40+ }41+ });42+ });43+ req.end();44+}45+46+/**47+ * Sort repositories by watchers desc.48+ *49+ * @param {Array} repos50+ * @api public51+ */52+53+function sort(repos){54+ return repos.sort(function(a, b){55+ if (a.watchers == b.watchers) return 0;56+ if (a.watchers > b.watchers) return -1;57+ if (a.watchers < b.watchers) return 1;58+ });59+}60+61+/**62+ * Tally up total watchers.63+ *64+ * @param {Array} repos65+ * @return {Number}66+ * @api public67+ */68+69+function totalWatchers(repos) {70+ return repos.reduce(function(sum, repo){71+ return sum + repo.watchers;72+ }, 0);73+}74+75+/**76+ * Default to my user name :)77+ */78+79+app.get('/', function(req, res){80+ res.redirect('/repos/visionmedia');81+});82+83+/**84+ * Display repos.85+ */86+87+app.get('/repos/:user', function(req, res, params, next){88+ var name = params.user;89+ request('/repos/show/' + name, function(err, user){90+ if (err) {91+ next(err)92+ } else {93+ res.render('index.jade', {94+ locals: {95+ totalWatchers: totalWatchers(user.repositories),96+ repos: sort(user.repositories),97+ name: name98+ }99+ });100+ }101+ });102+});103+104+// Serve statics from ./public105+app.use('/', connect.staticProvider(__dirname + '/public'));106+107+// Listen on port 3000108+app.listen(3000);