index.js
This file
Path
examples/params/index.js
74 lines · 1.3 KiB · JavaScript
Last changed by
refactor: improve readability (#6173)Shahan Arshadbefore adoptione4a61bd
History of this fileTake it
Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 1–2why, one step back1'use strict'2examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19
lines 3–6why, one step back3/**4 * Module dependencies.5 */6Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 7why, one step back7var createError = require('http-errors')examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19
lines 8–13why, one step back8var express = require('../../');9var app = module.exports = express();1011// Faux database1213var users = [Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 14why, one step back14 { name: 'tj' }examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19
lines 15–25why, one step back15 , { name: 'tobi' }16 , { name: 'loki' }17 , { name: 'jane' }18 , { name: 'bandit' }19];2021// Convert :to and :from to integers2223app.param(['to', 'from'], function(req, res, next, num, name){24 req.params[name] = parseInt(num, 10);25 if( isNaN(req.params[name]) ){Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 26why, one step back26 next(createError(400, 'failed to parseInt '+num));examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19
lines 27–34why, one step back27 } else {28 next();29 }30});3132// Load user by id3334app.param('user', function(req, res, next, id){refactor: improve readability (#6173)Shahan Arshadbefore adoptione4a61bd
lines 35–36why, one step back35 req.user = users[id]36 if (req.user) {examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19
lines 37–38why, one step back37 next();38 } else {Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 39why, one step back39 next(createError(404, 'failed to find user'));examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19
lines 40–54why, one step back40 }41});4243/**44 * GET index.45 */4647app.get('/', function(req, res){48 res.send('Visit /user/0 or /users/0-2');49});5051/**52 * GET :user.53 */54Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 55why, one step back55app.get('/user/:user', function (req, res) {examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19
lines 56–62why, one step back56 res.send('user ' + req.user.name);57});5859/**60 * GET users :from - :to.61 */62Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 63why, one step back63app.get('/users/:from-:to', function (req, res) {examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19
lines 64–66why, one step back64 var from = req.params.from;65 var to = req.params.to;66 var names = users.map(function(user){ return user.name; });Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 67why, one step back67 res.send('users ' + names.slice(from, to + 1).join(', '));examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19
lines 68–74why, one step back68});6970/* istanbul ignore next */71if (!module.parent) {72 app.listen(3000);73 console.log('Express started on port 3000');74}