Redirect the requesting user-agent to the given absolute or relative url.
return res.redirect([status,] url);
| Argument | Type | Details | |
|---|---|---|---|
| 1 | status |
(optional) a positive integer that corresponds to an HTTP status code. Defaults to 302 ("found"). | |
| 2 | url |
A URL expression (see below for complete specification). e.g. "http://google.com" or "/login" |
Sails/Express support a few forms of redirection, first being a fully qualified URI for redirecting to a different domain:
return res.redirect('http://google.com');
The second form is the domain-relative redirect. For example, if you were on http://example.com/admin/post/new, the following redirect to /admin would land you at http://example.com/admin:
return res.redirect('/checkout');
Pathname relative redirects are also possible. If you were on http://example.com/admin/post/new, the following redirect would land you at http//example.com/admin/post:
return res.redirect('..');
The final special-case is a back redirect, which allows you to redirect a request back where it came from using the "Referer" (or "Referrer") header (if omitted, redirects to / by default)
return res.redirect('back');
If you want to send a custom status code along with a redirect, you can do so by sending the status as the first argument to res.redirect:
return res.redirect(301, '/foo');
- This method is terminal, meaning it is generally the last line of code your app should run for a given request (hence the advisory usage of
returnthroughout these docs).- When your app calls
res.redirect(), Sails sends a response with status code 302. 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).- The Sails socket client does not follow redirects, so if an action is called via a websocket request using (for example)
io.socket.get(), it will simply receive a 302 status code and a 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.