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

Time:11-25

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?

CodePudding user response:

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>`)
})
  • Related