Skip to content
Myr

app.js

This file

Path

examples/github/app.js

116 lines · 2.5 KiB · JavaScript

Last changed by

Updated examplesTj Holowaychukbefore adoptionf60bcca

History of this file

Take it

Merge branch 'github' into connectTj Holowaychukbefore adoption59ac3d1

lines 1–9why, one step back
1
2/**3 * Module dependencies.4 */56var express = require('./../../lib/express'),7 connect = require('connect'),8 http = require('http');9

Removed connect.redirect()Tj Holowaychukbefore adoption0fce93b

line 10why, one step back
10var app = express.createServer();

Merge branch 'github' into connectTj Holowaychukbefore adoption59ac3d1

lines 11–81why, one step back
11
12// Expose our views1314app.set('views', __dirname + '/views');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.addListener('response', function(res){28 res.body = '';29 res.addListener('data', function(chunk){ res.body += chunk; });30 res.addListener('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 */81

Updated examplesTj Holowaychukbefore adoptionf60bcca

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

Added compare view to github exampleTj Holowaychukbefore adoption1581267

lines 84–100why, one step back
84 users = [];
85 (function fetchData(name){86 // We have a user name87 if (name) {88 console.log('... fetching %s', 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 users

Merge branch 'github' into connectTj Holowaychukbefore adoption59ac3d1

line 101why, one step back
101 } else {

Added compare view to github exampleTj Holowaychukbefore adoption1581267

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

Merge branch 'github' into connectTj Holowaychukbefore adoption59ac3d1

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

Added compare view to github exampleTj Holowaychukbefore adoption1581267

line 105why, one step back
105 users: users

Merge branch 'github' into connectTj Holowaychukbefore adoption59ac3d1

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

Added compare view to github exampleTj Holowaychukbefore adoption1581267

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

Merge branch 'github' into connectTj Holowaychukbefore adoption59ac3d1

lines 110–112why, one step back
110});
111112// Serve statics from ./public

Removed more use("/", ...)sTj Holowaychukbefore adoption8a36c54

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

Merge branch 'github' into connectTj Holowaychukbefore adoption59ac3d1

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