QA
Beginner LevelExpress JS

How does res.redirect() work and what is the difference between 301 Permanent and 302 Temporary redirects?

❓ Interview Prompt: Explain res.redirect(), setting Location headers, and comparing 301 Permanent vs 302 Temporary HTTP redirect status codes.

'res.redirect([status], url)' issues an HTTP redirect by sending a 'Location' header to the browser.

Redirect Status Codes:

•
302 Found (Default): Temporary redirect. Search engines preserve the original URL in indexing.
•
301 Moved Permanently: Permanent redirect ('res.redirect(301, "/new-url")'). Browsers and search engines cache the new URL permanently and transfer SEO link equity.

Code Implementation Example

javascript
import express from 'express';
const app = express();

// 1. Default 302 Temporary Redirect
app.get('/old-route', (req, res) => {
  res.redirect('/new-route');
});

// 2. Explicit 301 Permanent Redirect (for domain/SEO migrations)
app.get('/legacy-page', (req, res) => {
  res.redirect(301, '/permanent-page');
});
💡Executing temporary 302 and permanent 301 redirects using res.redirect.
🎯 Interview Key Takeaway

res.redirect() defaults to 302 temporary redirect. Specify 301 status explicitly for permanent SEO redirects.

#res.redirect#Redirects#HTTP Status#Beginner
Back to all Express JS Questions→

More Express JS Interview Questions

Beginner LevelExpress JS

What is Express.js and how does the middleware pipeline work?

Beginner LevelExpress JS

What is the difference between req.params, req.query, and req.body?

Beginner LevelExpress JS

How does express.Router() work for building modular route controllers?