如何将用户重定向到登录页面,并在用户登录后再次基于布尔变量将用户重定向到任何网址?
例如:
app.get('*', (req, res, next) => {
if(req.path === ''){
res.redirect(302, '/login')
//After login in, I need to check the boolean flag and based on that redirect the user to a specific url
if(flag) {
res.redirect(302, '/error')
}
} else {
next();
}
})
使用此实现,我收到以下错误:
'错误 [ERR_HTTP_HEADERS_SENT] 发送到客户端后无法设置标头'
有什么建议吗?
答案 0 :(得分:0)
如果您的标志为真,您将发送 2 个响应。你应该这样做:
app.get('*', (req, res, next) => {
if(req.path === ''){
if(flag) {
res.redirect(302, '/error')
} else {
res.redirect(302, '/login')
}
} else {
next();
}
})