Nest
Nest

How can we solve "res.redirect('back')" failures post-Express v5 upgrade in NestJS?

March 18, 2026

Express v5 completely removed the magic res.redirect('back') feature now you must manually grab the referrer header with req.get('Referrer') to redirect to the previous page.​

NestJS 11+ uses Express v5 by default, so res.redirect('back') just fails silently or goes to root. Replace it with res.redirect(req.get('Referrer') || '/') to get the actual previous page, or use NestJS's @Req() decorator to access the request object safely. Pro tip: Check for Helmet middleware—it might block referrers too

Code

//OLD (broken in Express v5)
import { Res } from '@nestjs/common';
import { Response } from 'express';

@Post('login')
login(@Res() res: Response) {
  // This fails post-Express v5 upgrade
  res.redirect('back'); 
}

//NEW (Express v5 compatible)
@Post('login')
login(@Req() req: Request, @Res() res: Response) {
  const referrer = req.get('Referrer') || '/dashboard';
  res.redirect(referrer);
}

// Or even cleaner with NestJS redirect helper
@Post('login')
login(@Req() req: Request) {
  const referrer = req.get('Referrer') || '/';
  return { redirect: referrer }; // Frontend handles it
}
      
Hire Now!

Need Help with Nest Development ?

Ready to leverage the power of conversational AI? Start your project with Zignuts expert AI developers.
bg-image
download-image
Company Deck
PDF, 3MB
© 2026 Zignuts Technolab. All Rights Reserved.
branch imagesbranch imagesbranch imagesbranch imagesbranch imagesbranch images