Skip to content
Myr

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 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–37why, one step back
27 } 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 back
38 next(createError(404, 'failed to find user'));

examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19

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

examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19

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