app.js
This file
Path
examples/params/app.js
70 lines · 1.2 KiB · JavaScript
Last changed by
Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
History of this fileTake it
The walk stopped after 200 commits: lines older than f41d09a point at it.
remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 1–4why, one step back1/**2 * Module dependencies.3 */4Merge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff
lines 5–6why, one step back5var express = require('../../');6var app = module.exports = express();remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 7–19why, one step back78// Faux database910var users = [11 { name: 'tj' }12 , { name: 'tobi' }13 , { name: 'loki' }14 , { name: 'jane' }15 , { name: 'bandit' }16];1718// Convert :to and :from to integers19Merge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff
line 20why, one step back20app.param(['to', 'from'], function(req, res, next, num, name){Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
lines 21–22why, one step back21 req.params[name] = parseInt(num, 10);22 if( isNaN(req.params[name]) ){remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 23–59why, one step back23 next(new Error('failed to parseInt '+num));24 } else {25 next();26 }27});2829// Load user by id3031app.param('user', function(req, res, next, id){32 if (req.user = users[id]) {33 next();34 } else {35 next(new Error('failed to find user'));36 }37});3839/**40 * GET index.41 */4243app.get('/', function(req, res){44 res.send('Visit /user/0 or /users/0-2');45});4647/**48 * GET :user.49 */5051app.get('/user/:user', function(req, res, next){52 res.send('user ' + req.user.name);53});5455/**56 * GET users :from - :to.57 */5859app.get('/users/:from-:to', function(req, res, next){Merge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff
lines 60–62why, one step back60 var from = req.params.from;61 var to = req.params.to;62 var names = users.map(function(user){ return user.name; });remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 63–65why, one step back63 res.send('users ' + names.slice(from, to).join(', '));64});65Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
line 66why, one step back66/* istanbul ignore next */remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 67–70why, one step back67if (!module.parent) {68 app.listen(3000);69 console.log('Express started on port 3000');70}