简体   繁体   中英

use next('route') and get 'undefined' response in next route in express

const express = require('express')
const app = express()

app.get('/user/:uid', (req, res, next) => {
  if (req.params.uid === 'lai9fox') next('route')
  else next()
}, (req, res, next) => {
  res.send(`<h1>hello, ${req.params.uid}</h1>`)
})

app.get('/user/:id', (req, res, next) => {
  res.send(`<h1>Welcome you, ${req.params.uid} !</h1>`)
})

app.listen(3000, () => console.log('server is running at port 3000...'))

When I visit http://localhost:3000/user/lai , it correctly shows:

hello, lai

But when I visit http://localhost:3000/user/lai9fox , it shows:

Welcome you, undefined!

What's go wrong?

you need to change the req.params.uid for id

app.get('/user/:id', (req, res, next) => {
  res.send(`<h1>Welcome you, ${req.params.id} !</h1>`)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM