Skip to content
Myr

app.js

This file

Path

examples/github/app.js

116 lines · 2.4 KiB · JavaScript

Last changed by

fixing more examplesTj Holowaychukbefore adoption66a6214

History of this file

Take it

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

Merge branch 'views'Tj Holowaychukbefore adoption0ed121d

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

fixing more examplesTj Holowaychukbefore adoption66a6214

line 9why, one step back
9var express = require('../../lib/express')

fixed github exampleTj Holowaychukbefore adoptionf5af66a

line 10why, one step back
10 , http = require('http');

Merge branch 'views'Tj Holowaychukbefore adoption0ed121d

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');

Merge branch 'views'Tj Holowaychukbefore adoption0ed121d

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–29why, one step back
28 var client = http.createClient(80, 'github.com')
29 , req = client.request('GET', '/api/v2/json' + path, { Host: 'github.com' });

fixing more examplesTj Holowaychukbefore adoption66a6214

line 30why, one step back
30 req.on('response', function(res){

fixed github exampleTj Holowaychukbefore adoptionf5af66a

line 31why, one step back
31 res.body = '';

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 32–33why, one step back
32 res.on('data', function(chunk){ res.body += chunk; });
33 res.on('end', function(){

fixed github exampleTj Holowaychukbefore adoptionf5af66a

lines 34–38why, one step back
34 try {
35 fn(null, JSON.parse(res.body));36 } catch (err) {37 fn(err);38 }

Merge branch 'views'Tj Holowaychukbefore adoption0ed121d

line 39why, one step back
39 });

fixed github exampleTj Holowaychukbefore adoptionf5af66a

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

Merge branch 'views'Tj Holowaychukbefore adoption0ed121d

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 });

Merge branch 'views'Tj Holowaychukbefore adoption0ed121d

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);

Merge branch 'views'Tj Holowaychukbefore adoption0ed121d

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');

Merge branch 'views'Tj Holowaychukbefore adoption0ed121d

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)

Merge branch 'views'Tj Holowaychukbefore adoption0ed121d

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());

Merge branch 'views'Tj Holowaychukbefore adoption0ed121d

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());

Merge branch 'views'Tj Holowaychukbefore adoption0ed121d

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

fixing more examplesTj Holowaychukbefore adoption66a6214

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

Merge branch 'views'Tj Holowaychukbefore adoption0ed121d

lines 113–116why, one step back
113
114// Listen on port 3000115app.listen(3000);116console.log('Express app started on port 3000');