Bird
Raised Fist0
Expressframework~5 mins

res.redirect for redirections in Express - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does res.redirect() do in Express?

res.redirect() sends a response to the browser telling it to go to a different URL. It makes the browser load a new page.

Click to reveal answer
beginner
How do you redirect a user to the homepage '/' using res.redirect()?

Use res.redirect('/'). This tells the browser to go to the root page of the website.

Click to reveal answer
intermediate
What HTTP status code does res.redirect() use by default?

By default, res.redirect() uses status code 302, which means "Found" or temporary redirect.

Click to reveal answer
intermediate
How can you specify a permanent redirect with res.redirect()?

Pass the status code 301 as the first argument, then the URL: res.redirect(301, '/new-url'). This tells browsers the redirect is permanent.

Click to reveal answer
beginner
Can res.redirect() redirect to an external website?

Yes, you can redirect to any URL, including external sites like res.redirect('https://example.com').

Click to reveal answer
What is the default HTTP status code used by res.redirect() in Express?
A500
B301
C404
D302
How do you redirect a user permanently to '/dashboard'?
Ares.redirect(301, '/dashboard')
Bres.redirect(500, '/dashboard')
Cres.redirect(404, '/dashboard')
Dres.redirect('/dashboard')
Which of these is a valid way to redirect to an external site?
Ares.redirect(404, 'https://google.com')
Bres.redirect('/google.com')
Cres.redirect('https://google.com')
Dres.redirect('google.com')
What happens after calling res.redirect() in your route handler?
AThe server sends a redirect response and ends the request.
BThe server continues processing the route.
CThe server throws an error.
DNothing happens until you call <code>res.send()</code>.
Which method is used to redirect in Express?
Ares.sendRedirect()
Bres.redirect()
Cres.go()
Dres.move()
Explain how to use res.redirect() to send a temporary and a permanent redirect in Express.
Think about the status codes and how they affect browser behavior.
You got /3 concepts.
    Describe what happens in the browser and server when res.redirect('/home') is called.
    Consider the communication between server and browser.
    You got /3 concepts.

      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