B. Returning a string after res.redirect() causes an error
C. Missing await before res.redirect()
D. res.redirect() must be called without parameters
Solution
Step 1: Understand res.redirect() behavior
Calling res.redirect() sends the response immediately. Returning a value after sending response causes NestJS to throw an error because response is already sent.
Step 2: Check other options
Status code 301 is valid for permanent redirect, no await needed as redirect is synchronous, and redirect requires parameters.
Final Answer:
Returning a string after res.redirect() causes an error -> Option B
Quick Check:
Don't return after sending response [OK]
Hint: Don't return after res.redirect() sends response [OK]
Common Mistakes:
Thinking 301 is invalid status code
Adding await unnecessarily
Calling redirect without parameters
5. You want to create a NestJS controller method that redirects users to different URLs based on a query parameter type. If type is 'admin', redirect to '/admin/dashboard' with status 302; if 'user', redirect to '/user/home' with status 301; otherwise, redirect to '/' with status 302. Which code snippet correctly implements this?
D. async redirect(@Query('type') type: string, @Res() res) {
if (type === 'admin') {
res.redirect('/admin/dashboard');
} else if (type === 'user') {
res.redirect(301, '/user/home');
} else {
res.redirect('/');
}
}
Solution
Step 1: Check correct usage of res.redirect()
Calling res.redirect() with URL only defaults to 302 status. To specify status, pass it as first argument. The method sends response immediately, so no return is needed.
Step 2: Analyze each option
async redirect(@Query('type') type: string, @Res() res) {
if (type === 'admin') {
return res.redirect('/admin/dashboard');
} else if (type === 'user') {
return res.redirect(301, '/user/home');
} else {
return res.redirect('/');
}
} incorrectly uses return res.redirect(), which can cause errors. async redirect(@Query('type') type: string, @Res() res) {
if (type === 'admin') {
res.redirect(302, '/admin/dashboard');
} else if (type === 'user') {
res.redirect('/user/home');
} else {
res.redirect(302, '/');
}
return 'Redirected';
} returns a string after redirect, causing errors. async redirect(@Query('type') type: string, @Res() res) {
if (type === 'admin') {
return res.redirect(302, '/admin/dashboard');
} else if (type === 'user') {
return res.redirect('/user/home');
} else {
return res.redirect(302, '/');
}
} mixes return and missing status codes. async redirect(@Query('type') type: string, @Res() res) {
if (type === 'admin') {
res.redirect('/admin/dashboard');
} else if (type === 'user') {
res.redirect(301, '/user/home');
} else {
res.redirect('/');
}
} calls res.redirect() properly without return and uses correct status codes.
Final Answer:
The code that calls res.redirect() properly without return and with correct status codes -> Option D
Quick Check:
Call res.redirect() without return, specify status if needed [OK]
Hint: Call res.redirect() without return; specify status as first argument [OK]
Common Mistakes:
Returning after res.redirect() causing errors
Not specifying status code correctly
Returning strings after sending response
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.