【发布时间】:2021-08-30 23:51:17
【问题描述】:
我想在 serverMiddleware 中为一条路由重定向,它说重定向不是函数
代码:
export default function (req, res, next) {
// req is the Node.js http request object
const vuex = req.headers.cookie.slice(5);
if (vuex.includes("auth%22:true")) {
res.redirect(301, '/campaigns')
}
next()
}
我也尝试过使用res.writeHead,但效果不佳。 res.writeHead 给了我这个错误信息:
Cannot set headers after they are sent to the client 如果我把 next() 放在 else 语句中,它会说 The page isn’t redirecting properly
代码:
export default function (req, res, next) {
// req is the Node.js http request object
const vuex = req.headers.cookie.slice(5);
if (vuex.includes("auth%22:true")) {
res.writeHead(301, {
Location: '/campaigns'
})
res.end()
} else {
next()
}
}
【问题讨论】:
标签: nuxt.js middleware