Skip to content
Myr

app.js

This file

Path

examples/github/app.js

119 lines · 2.7 KiB · JavaScript

Last changed by

Exposing ./support libs to examplesTj Holowaychukbefore adoption9b7470d

History of this file

Take it

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

Blog example using "home"Tj Holowaychukbefore adoptionc3a2aff

line 1why, one step back
1

Exposing ./support libs to examplesTj Holowaychukbefore adoption9b7470d

lines 2–4why, one step back
2// Expose modules in ./support for demo purposes
3require.paths.unshift(__dirname + '/../../support');4

Blog example using "home"Tj Holowaychukbefore adoptionc3a2aff

lines 5–83why, one step back
5/**
6 * Module dependencies.7 */89var express = require('./../../lib/express'),10 http = require('http');1112var app = express.createServer();1314// Expose our views1516app.set('views', __dirname + '/views');1718/**19 * Request github json api `path`.20 *21 * @param {String} path22 * @param {Function} fn23 * @api public24 */2526function request(path, fn){27 var client = http.createClient(80, 'github.com'),28 req = client.request('GET', '/api/v2/json' + path, { Host: 'github.com' });29 req.addListener('response', function(res){30 res.body = '';31 res.addListener('data', function(chunk){ res.body += chunk; });32 res.addListener('end', function(){33 try {34 fn(null, JSON.parse(res.body));35 } catch (err) {36 fn(err);37 }38 });39 });40 req.end();41}4243/**44 * Sort repositories by watchers desc.45 *46 * @param {Array} repos47 * @api public48 */4950function sort(repos){51 return repos.sort(function(a, b){52 if (a.watchers == b.watchers) return 0;53 if (a.watchers > b.watchers) return -1;54 if (a.watchers < b.watchers) return 1;55 });56}5758/**59 * Tally up total watchers.60 *61 * @param {Array} repos62 * @return {Number}63 * @api public64 */6566function totalWatchers(repos) {67 return repos.reduce(function(sum, repo){68 return sum + repo.watchers;69 }, 0);70}7172/**73 * Default to my user name :)74 */7576app.get('/', function(req, res){77 res.redirect('/repos/visionmedia');78});7980/**81 * Display repos.82 */83

Updated examplesTj Holowaychukbefore adoptionf60bcca

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

Blog example using "home"Tj Holowaychukbefore adoptionc3a2aff

lines 86–89why, one step back
86 users = [];
87 (function fetchData(name){88 // We have a user name89 if (name) {

Exposing ./support libs to examplesTj Holowaychukbefore adoption9b7470d

line 90why, one step back
90 console.log('... fetching \x1b[33m%s\x1b[0m', name);

Blog example using "home"Tj Holowaychukbefore adoptionc3a2aff

lines 91–114why, one step back
91 request('/repos/show/' + name, function(err, user){
92 if (err) {93 next(err)94 } else {95 user.totalWatchers = totalWatchers(user.repositories);96 user.repos = sort(user.repositories);97 user.name = name;98 users.push(user);99 fetchData(names.shift());100 }101 });102 // No more users103 } else {104 console.log('... done');105 res.render('index.jade', {106 locals: {107 users: users108 }109 });110 }111 })(names.shift());112});113114// Serve statics from ./public

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

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

Blog example using "home"Tj Holowaychukbefore adoptionc3a2aff

lines 116–118why, one step back
116
117// Listen on port 3000118app.listen(3000);

Exposing ./support libs to examplesTj Holowaychukbefore adoption9b7470d

line 119why, one step back
119console.log('Express app started on port 3000');