Node version: 10.16.3
Sails version _(sails)_: 1.2.3
I try to execute res.redirect ('/'), but console.log gives meundefined and also I get the error
error: Sending 500 ("Server Error") response:
聽 Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
聽聽聽聽 at ServerResponse.setHeader (_http_outgoing.js: 470: 11)
my code:
module.exports = {
testq: function (req, res) {
console.log('redirect', res.redirect('/'));
return res.redirect('/')
}
}
``
// config\routes.js
module.exports.routes = {
'/': {view: 'pages/homepage'},
}
Please tell me why res.redirect not redirecting and in what cases this error may occur?
@alex-jss Thanks for posting! We'll take a look as soon as possible.
In the mean time, there are a few ways you can help speed things along:
Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.
For help with questions about Sails, click here.
Hi, @alex-jss! I'd like a bit more context to better understand your issue. Would you mind providing a minimal repo that reproduces this error? Thanks!
repo unfortunately there is no way to provide. I have provided all possible information.
The challenge is to redirect to any given page
I changed my redirect request to another page. In the developer panel - network, I get preview of page I want, but the error remains and the redirect is not performed.
my code:
module.exports = {
testq: function (req, res) {
console.log('redirect', res.redirect('/views/index/index.html'));
res.redirect('/views/index/index.html')
}
}
I attach a mistake
err_redirect.txt
Hey, @alex-jss!
I've got a theory about your issue.
The error your getting indicates that you're setting headers twice. At first, I didn't see where that was happening, but I'm realizing now that the line
console.log('redirect', res.redirect('/'));,
_does_ actually call res.redirect(), which sends headers. This means that on the next line,
return res.redirect('/');
headers are being sent again, which isn't allowed. I believe this is where your error is coming from.
@DominusKelvin, always a good idea to take another gander at the docs. Good thought!
Thanks, @madisonhicks. It really works. I thought that res.redirect () works differently and it automatically redirects the user in the browser to the necessary page.