balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.83k stars 1.95k forks source link

res.redirect not redirecting #6929

Closed ghost closed 4 years ago

ghost commented 4 years ago

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?

sailsbot commented 4 years ago

@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.

whichking commented 4 years ago

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!

ghost commented 4 years ago

repo unfortunately there is no way to provide. I have provided all possible information. The challenge is to redirect to any given page

ghost commented 4 years ago

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

DominusKelvin commented 4 years ago

I think you should check the Notes section of the docs for a possible help. You seem to be doing everything nicely but just to make sure. Check the docs

whichking commented 4 years ago

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.

whichking commented 4 years ago

@DominusKelvin, always a good idea to take another gander at the docs. Good thought!

ghost commented 4 years ago

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.