Skip to content
Myr

app.js

This file

Path

examples/params/app.js

70 lines · 1.2 KiB · JavaScript

Last changed by

Updated params example with acceptance test.Robert Sköldbefore adoptiona153082

History of this file

Take it

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

Added app.render() local precedence testTj Holowaychukbefore adoptionf98c112

lines 1–5why, one step back
1
2/**3 * Module dependencies.4 */5

Updated params example with acceptance test.Robert Sköldbefore adoptiona153082

lines 6–7why, one step back
6var express = require('../../')
7 , app = module.exports = express();

Added app.render() local precedence testTj Holowaychukbefore adoptionf98c112

lines 8–20why, one step back
8
9// Faux database1011var users = [12 { name: 'tj' }13 , { name: 'tobi' }14 , { name: 'loki' }15 , { name: 'jane' }16 , { name: 'bandit' }17];1819// Convert :to and :from to integers20

Updated params example with acceptance test.Robert Sköldbefore adoptiona153082

lines 21–28why, one step back
21app.param(['to', 'from'], function(req, res, next, num, name){
22 req.params[name] = num = parseInt(num, 10);23 if( isNaN(num) ){24 next(new Error('failed to parseInt '+num));25 } else {26 next();27 }28});

Added app.render() local precedence testTj Holowaychukbefore adoptionf98c112

lines 29–66why, one step back
29
30// Load user by id3132app.param('user', function(req, res, next, id){33 if (req.user = users[id]) {34 next();35 } else {36 next(new Error('failed to find user'));37 }38});3940/**41 * GET index.42 */4344app.get('/', function(req, res){45 res.send('Visit /user/0 or /users/0-2');46});4748/**49 * GET :user.50 */5152app.get('/user/:user', function(req, res, next){53 res.send('user ' + req.user.name);54});5556/**57 * GET users :from - :to.58 */5960app.get('/users/:from-:to', function(req, res, next){61 var from = req.params.from62 , to = req.params.to63 , names = users.map(function(user){ return user.name; });64 res.send('users ' + names.slice(from, to).join(', '));65});66

Updated params example with acceptance test.Robert Sköldbefore adoptiona153082

lines 67–70why, one step back
67if (!module.parent) {
68 app.listen(3000);69 console.log('Express started on port 3000');70}