Bird
Raised Fist0
Expressframework~10 mins

res.redirect for redirections in Express - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to redirect the user to '/home'.

Express
app.get('/start', (req, res) => {
  res.[1]('/home');
});
Drag options to blanks, or click blank then click option'
Asend
Brender
Credirect
Djson
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.send instead of res.redirect
Using res.render which is for templates
2fill in blank
medium

Complete the code to redirect with a 301 status code to '/new-page'.

Express
app.get('/old-page', (req, res) => {
  res.status(301).[1]('/new-page');
});
Drag options to blanks, or click blank then click option'
Aredirect
Bsend
Cjson
Drender
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.send instead of res.redirect
Not setting the status code before redirect
3fill in blank
hard

Fix the error in the code to properly redirect to '/dashboard'.

Express
app.get('/login', (req, res) => {
  res.redirect [1] '/dashboard';
});
Drag options to blanks, or click blank then click option'
A()
B[]
C{}
D<>
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting parentheses when calling res.redirect
Using square or curly brackets instead of parentheses
4fill in blank
hard

Fill both blanks to redirect with a 302 status code to '/profile'.

Express
app.get('/user', (req, res) => {
  res.[1]([2], '/profile');
});
Drag options to blanks, or click blank then click option'
Aredirect
Bsend
C302
D301
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.send instead of res.redirect
Using wrong status code like 301 for temporary redirect
5fill in blank
hard

Fill all three blanks to redirect to '/logout' only if user is logged in.

Express
app.get('/exit', (req, res) => {
  if (req.user) {
    res.[1]([2], [3]);
  } else {
    res.send('Not logged in');
  }
});
Drag options to blanks, or click blank then click option'
Aredirect
B302
C'/logout'
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.send instead of res.redirect
Forgetting quotes around the URL string
Using wrong status code

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