我正试图根据前端输入的表单在express中执行错误管理。我看到要么我可以将res.status(401)
作为错误代码发送,要么res.json({})
作为发送错误消息发送,但不能两者都发送。我应该在下面做些什么来同时发送两者
app.post('/verifyOTP', (req, res) => {
const hash = req.body.hash;
let [ hashValue, expires ] = hash.split('.');
let now = Date.now();
if (now > parseInt(expires)) {
return res.status(400).json({ error: 'Timeout. Please try again' })
}
})
您可以随时执行此
启用send-to-send对象的功能:-当参数是Array或object时,Express会使用JSON表示进行响应:有关发送的更多参考Express文档
如何组合.status and .send?
:-设置响应的HTTP状态。它是Node的响应的可链接别名。statusCode。获取更多参考express状态文档
app.post('/verifyOTP', (req, res) => {
const hash = req.body.hash;
let [ hashValue, expires ] = hash.split('.');
let now = Date.now();
if (now > parseInt(expires)) {
return res.status(400).send({ error: 'Timeout. Please try again' })
}
})
如文档中所述:
res.status(400).send({ error: 'Timeout. Please try again' })