Nest

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

March 18, 2026

download ready
Thank You
Your submission has been received.
We will be in touch and contact you soon!

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 ?

Work with our skilled nest developers to accelerate your project and boost its performance.
**Hire now**Hire Now**Hire Now**Hire now**Hire now

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

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
}