0
0
Expressframework~10 mins

res.redirect for redirections in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - res.redirect for redirections
Client sends request
Server receives request
Server calls res.redirect(url)
Server sends 302 status + Location header
Client receives redirect response
Client automatically requests new URL
New resource served to client
This flow shows how a server tells the browser to go to a new URL using res.redirect, causing the browser to request that new URL.
Execution Sample
Express
app.get('/old-page', (req, res) => {
  res.redirect('/new-page');
});
When a client requests '/old-page', the server responds by redirecting them to '/new-page'.
Execution Table
StepActionServer ResponseClient Behavior
1Client requests '/old-page'No response yetSends HTTP GET to server
2Server receives requestNo response yetWaiting for server response
3Server calls res.redirect('/new-page')Sends HTTP 302 status with Location: /new-page headerReceives redirect response
4Client receives 302 redirectResponse sentAutomatically requests '/new-page' URL
5Server receives new request for '/new-page'Processes and sends resourceReceives new page content
6Client renders '/new-page' contentResponse completeDisplays new page to user
💡 Redirect completed when client requests and receives the new URL content.
Variable Tracker
VariableStartAfter Step 3After Step 5Final
requestedURLundefined/old-page/new-page/new-page
responseStatusundefined302200200
responseHeaders.Locationundefined/new-pageundefinedundefined
Key Moments - 2 Insights
Why does the client make a second request after res.redirect is called?
Because res.redirect sends a 302 status with a Location header telling the client to go to a new URL, so the client automatically requests that new URL (see execution_table step 4).
Does res.redirect send the content of the new page immediately?
No, it only sends a redirect response (302 status and Location header). The client must request the new page separately (see execution_table steps 3 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what HTTP status code does the server send when res.redirect is called?
A404
B302
C200
D500
💡 Hint
Check the 'Server Response' column at step 3 in the execution_table.
At which step does the client send the request for the new URL after redirection?
AStep 4
BStep 5
CStep 2
DStep 6
💡 Hint
Look at the 'Client Behavior' column to see when the new URL request happens.
If the server changed res.redirect('/new-page') to res.redirect(301, '/new-page'), what would change in the execution table?
AThe client would not request the new URL
BThe server would send the new page content immediately
CThe status code at step 3 would be 301 instead of 302
DNo change at all
💡 Hint
Focus on the 'Server Response' status code at step 3.
Concept Snapshot
res.redirect(url) sends a 302 redirect response to the client.
The client then requests the new URL automatically.
The server does NOT send the new page content immediately.
Use res.redirect(status, url) to specify other redirect codes like 301.
Commonly used to move users from old URLs to new ones.
Full Transcript
When a client asks the server for a page, the server can tell the client to go somewhere else by using res.redirect. This sends a special response with status 302 and a Location header. The client sees this and automatically asks for the new page. The server then sends the new page content. This process helps move users smoothly from one URL to another without confusion.