Skip to content
Myr

app.js

This file

Path

examples/params/app.js

79 lines · 1.4 KiB · JavaScript

Last changed by

deps: supertest@1.0.1Douglas Christopher Wilsonbefore adoption7df8fa3

History of this file

Take it

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

3.15.1Douglas Christopher Wilsonbefore adoption9f4968a

lines 1–18why, one step back
1
2/**3 * Module dependencies.4 */56var express = require('../../')7 , app = module.exports = express();89// Faux database1011var users = [12 { name: 'tj' }13 , { name: 'tobi' }14 , { name: 'loki' }15 , { name: 'jane' }16 , { name: 'bandit' }17];18

deps: supertest@1.0.1Douglas Christopher Wilsonbefore adoption7df8fa3

lines 19–26why, one step back
19// Create HTTP error
2021function createError(status, message) {22 var err = new Error(message);23 err.status = status;24 return err;25}26

3.15.1Douglas Christopher Wilsonbefore adoption9f4968a

lines 27–31why, one step back
27// Convert :to and :from to integers
2829app.param(['to', 'from'], function(req, res, next, num, name){ 30 req.params[name] = parseInt(num, 10);31 if( isNaN(req.params[name]) ){

deps: supertest@1.0.1Douglas Christopher Wilsonbefore adoption7df8fa3

line 32why, one step back
32 next(createError(400, 'failed to parseInt '+num));

3.15.1Douglas Christopher Wilsonbefore adoption9f4968a

lines 33–43why, one step back
33 } else {
34 next();35 }36});3738// Load user by id3940app.param('user', function(req, res, next, id){41 if (req.user = users[id]) {42 next();43 } else {

deps: supertest@1.0.1Douglas Christopher Wilsonbefore adoption7df8fa3

line 44why, one step back
44 next(createError(404, 'failed to find user'));

3.15.1Douglas Christopher Wilsonbefore adoption9f4968a

lines 45–79why, one step back
45 }
46});4748/**49 * GET index.50 */5152app.get('/', function(req, res){53 res.send('Visit /user/0 or /users/0-2');54});5556/**57 * GET :user.58 */5960app.get('/user/:user', function(req, res, next){61 res.send('user ' + req.user.name);62});6364/**65 * GET users :from - :to.66 */6768app.get('/users/:from-:to', function(req, res, next){69 var from = req.params.from70 , to = req.params.to71 , names = users.map(function(user){ return user.name; });72 res.send('users ' + names.slice(from, to).join(', '));73});7475/* istanbul ignore next */76if (!module.parent) {77 app.listen(3000);78 console.log('Express started on port 3000');79}