Skip to content
Myr

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 file

Take it

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

lines 1–2why, one step back
1'use strict'
2

examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 7why, one step back
7var createError = require('http-errors')

examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19

lines 8–13why, one step back
8var express = require('../../');
9var app = module.exports = express();1011// Faux database1213var users = [

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

line 14why, one step back
14 { name: 'tj' }

examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19

lines 15–25why, one step back
15 , { 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 back
26 next(createError(400, 'failed to parseInt '+num));

examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19

lines 27–34why, one step back
27 } 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 back
35 req.user = users[id]
36 if (req.user) {

examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19

lines 37–38why, one step back
37 next();
38 } else {

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

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

examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19

lines 40–54why, one step back
40 }
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 */54

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 55why, one step back
55app.get('/user/:user', function (req, res) {

examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19

lines 56–62why, one step back
56 res.send('user ' + req.user.name);
57});5859/**60 * GET users :from - :to.61 */62

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 63why, one step back
63app.get('/users/:from-:to', function (req, res) {

examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19

lines 64–66why, one step back
64 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 back
67 res.send('users ' + names.slice(from, to + 1).join(', '));

examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19

lines 68–74why, one step back
68});
6970/* istanbul ignore next */71if (!module.parent) {72 app.listen(3000);73 console.log('Express started on port 3000');74}