【问题标题】:Express routes not working as they should特快路线无法正常工作
【发布时间】:2018-07-23 16:02:32
【问题描述】:

我有一个用于此登录的标准 expressroute。即使 req.body.password 不正确,我也会被重定向到 '/login'

router.post('/student/login', (req, res) => {
  if (req.body.password = 'password') {
    return res.status(200).redirect('/login')
  } else {
    return res.status(401).redirect('/landingpage')
  }
})

我在这里错过了什么?

【问题讨论】:

  • 应该是req.body.password === 'password'。使用req.body.password = 'password',您正在做一个分配,它总是会解析为true
  • 您在 if 语句中使用了单个 =,这会导致静默赋值,并且将始终执行 if 分支。
  • 啊当然!谢谢大家:D

标签: javascript express routes


【解决方案1】:

在第二行将 = 更改为 ===,因为您正在使用 = 分配一个值,并且正在将值与 === 进行比较

router.post('/student/login', (req, res) => {
  if (req.body.password === 'password') {
    return res.status(200).redirect('/login')
  } else {
    return res.status(401).redirect('/landingpage')
  }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 2020-01-28
    • 1970-01-01
    相关资源
    最近更新 更多