res.redirect()
Redirects the client to a URL with an optional status code.
Syntax
res.redirect([status,] path) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
status | number | No | The redirect status code (default 302). |
path | string | Yes | The target URL, relative path, or `'back'`. |
Returns
Response — The response object.
Examples
app.post('/login', (req, res) => {
res.redirect('/dashboard');
});
Output
HTTP/1.1 302 Found
Location: /dashboard
app.get('/old', (req, res) => {
res.redirect(301, '/new');
});
Output
HTTP/1.1 301 Moved Permanently
Location: /new
Notes
Use 301 for permanent moves (cached by browsers) and 302/303 for
temporary ones. After a POST, prefer 303 with a GET target
(Post/Redirect/Get). Validate user-supplied redirect targets to
prevent open-redirect attacks.