Edit Page

res.redirect()

Redirect the requesting user agent to the given absolute or relative URL.

Usage

#
return res.redirect(url);

Or:

Arguments

#
Argument Type Details
1 statusCode Number? An optional status code (e.g. 301). (If omitted, a status code of 302 will be assumed.)
2 url String A URL expression (see below for complete specification).
e.g. "http://google.com" or "/login"

Details

#

Sails/Express support a few forms of redirection:

return res.redirect('http://google.com');
return res.redirect('/checkout');
return res.redirect('..');
return res.redirect('back');

Notes

#
  • This method is terminal, meaning that it is generally the last line of code your app should run for a given request (hence the advisory usage of return throughout these docs).
  • As of Sails v1.x, for HTTP requests, res.redirect() does not respect the status code established by res.status(). Thanks @Guillaume-Duval and @oshatrk!
  • When your app calls res.redirect(), Sails sends a response with status code 302, indicating a temporary redirect. This instructs the user agent to send a new request to the indicated URL. There is no way to force a user agent to follow redirects, but most clients play nicely.
  • In general, you should not need to use res.redirect() if a request "wants JSON" (i.e. req.wantsJSON).
  • If a request originated from the Sails socket client, it always "wants JSON", so the Sails socket client does not follow redirects. For this reason, if an action is called via a WebSocket request using (for example) io.socket.get(), it will simply receive the appropriate status code and a "Location" header indicating the location of the desired resource. It’s up to the client-side code to decide how to handle redirects for WebSocket requests.

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

Reference