Bird
Raised Fist0
Expressframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does res.redirect('/home') do in an Express app?
easy
A. Sends a JSON response with '/home' data
B. Stops the server from running
C. Sends the user to the '/home' URL automatically
D. Logs '/home' to the console

Solution

  1. Step 1: Understand res.redirect purpose

    The res.redirect method sends an HTTP redirect response to the client, telling the browser to go to a new URL.
  2. Step 2: Analyze the argument '/home'

    Passing '/home' means the user will be redirected to the '/home' path on the same server.
  3. Final Answer:

    Sends the user to the '/home' URL automatically -> Option C
  4. Quick Check:

    res.redirect('/home') = Redirect to '/home' [OK]
Hint: res.redirect sends user to given URL automatically [OK]
Common Mistakes:
  • Thinking it sends JSON data
  • Confusing redirect with stopping server
  • Assuming it logs to console
2. Which is the correct syntax to redirect permanently to '/dashboard' in Express?
easy
A. res.redirect('/dashboard', 301);
B. res.redirect(301, '/dashboard');
C. res.redirectPermanent('/dashboard');
D. res.redirectPermanent(301, '/dashboard');

Solution

  1. Step 1: Recall res.redirect syntax for status code

    Express uses res.redirect(statusCode, url) to specify redirect type.
  2. Step 2: Identify correct order of arguments

    The status code (301) must come first, then the URL string.
  3. Final Answer:

    res.redirect(301, '/dashboard'); -> Option B
  4. Quick Check:

    Status code first, URL second = res.redirect(301, '/dashboard'); [OK]
Hint: Status code goes first in res.redirect(status, url) [OK]
Common Mistakes:
  • Swapping arguments order
  • Using non-existent res.redirectPermanent method
  • Passing status code as second argument
3. What will the browser do after this Express code runs?
app.get('/old', (req, res) => {
  res.redirect('/new');
});
medium
A. Show the content of '/old' page
B. Send a JSON response with '/new' data
C. Return a 404 error
D. Redirect the browser to '/new' URL

Solution

  1. Step 1: Understand route handler behavior

    The handler for '/old' calls res.redirect('/new'), which sends a redirect response.
  2. Step 2: Browser reaction to redirect

    The browser receives the redirect and automatically requests the '/new' URL instead of '/old'.
  3. Final Answer:

    Redirect the browser to '/new' URL -> Option D
  4. Quick Check:

    res.redirect('/new') = Browser goes to '/new' [OK]
Hint: res.redirect sends browser to new URL automatically [OK]
Common Mistakes:
  • Expecting original page content to show
  • Thinking it returns 404 error
  • Confusing redirect with JSON response
4. Find the error in this Express code snippet:
app.get('/start', (req, res) => {
  res.redirect('/end', 302);
});
medium
A. The status code should be first argument, not second
B. res.redirect cannot be used inside app.get
C. The URL must be absolute, not relative
D. 302 is not a valid redirect status code

Solution

  1. Step 1: Check res.redirect argument order

    Express expects res.redirect(statusCode, url), status code first.
  2. Step 2: Identify incorrect argument order

    The code uses res.redirect('/end', 302), which is reversed and causes unexpected behavior.
  3. Final Answer:

    The status code should be first argument, not second -> Option A
  4. Quick Check:

    Correct order: status, URL [OK]
Hint: Status code must come before URL in res.redirect [OK]
Common Mistakes:
  • Swapping status and URL arguments
  • Thinking res.redirect can't be used in routes
  • Believing relative URLs are invalid
  • Assuming 302 is invalid status
5. You want to redirect users from '/login' to '/dashboard' only if they are logged in. Which code correctly uses res.redirect to do this?
hard
A. if (req.user) { res.redirect('/dashboard'); } else { res.send('Please login'); }
B. res.redirect('/dashboard'); if (req.user) { res.send('Welcome'); }
C. res.redirect(302, '/dashboard'); res.send('Logged in');
D. if (!req.user) { res.redirect('/dashboard'); } else { res.send('Please login'); }

Solution

  1. Step 1: Check login condition usage

    The code must check if req.user exists to confirm login.
  2. Step 2: Redirect only if logged in

    If logged in, redirect to '/dashboard'; otherwise, send a message to login.
  3. Step 3: Verify correct use of res.redirect and res.send

    if (req.user) { res.redirect('/dashboard'); } else { res.send('Please login'); } correctly calls res.redirect inside the if block and sends response otherwise, avoiding multiple responses.
  4. Final Answer:

    if (req.user) { res.redirect('/dashboard'); } else { res.send('Please login'); } -> Option A
  5. Quick Check:

    Redirect only when logged in = if (req.user) { res.redirect('/dashboard'); } else { res.send('Please login'); } [OK]
Hint: Redirect inside if block after login check [OK]
Common Mistakes:
  • Calling res.redirect without condition
  • Sending multiple responses in one request
  • Redirecting when user is not logged in
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard