index.js
This file
Path
examples/params/index.js
73 lines · 1.3 KiB · JavaScript
Last changed by
Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
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–37why, one step back27 } else {28 next();29 }30});3132// Load user by id3334app.param('user', function(req, res, next, id){35 if (req.user = users[id]) {36 next();37 } else {Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 38why, one step back38 next(createError(404, 'failed to find user'));examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19
lines 39–53why, one step back39 }40});4142/**43 * GET index.44 */4546app.get('/', function(req, res){47 res.send('Visit /user/0 or /users/0-2');48});4950/**51 * GET :user.52 */53Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 54why, one step back54app.get('/user/:user', function (req, res) {examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19
lines 55–61why, one step back55 res.send('user ' + req.user.name);56});5758/**59 * GET users :from - :to.60 */61Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 62why, one step back62app.get('/users/:from-:to', function (req, res) {examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19
lines 63–65why, one step back63 var from = req.params.from;64 var to = req.params.to;65 var names = users.map(function(user){ return user.name; });Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 66why, one step back66 res.send('users ' + names.slice(from, to + 1).join(', '));examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19
lines 67–73why, one step back67});6869/* istanbul ignore next */70if (!module.parent) {71 app.listen(3000);72 console.log('Express started on port 3000');73}