Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Express Basics Deeper into Routing with Express Challenges: Redirects

Danish Saleem
Danish Saleem
7,965 Points

using render instead of redirect

hello , what if we use render instead of redirect from previous video challenge , like below

if (name) { res.render('index',{name}); } else { res.render('hello') } });

can anyone please help me out understanding the difference if i use res.render with else instead of res.redirect

1 Answer

Hi Danish,

You could do that but then the URL would be misleading to the user. There's also the possibility that you'd have to write duplicate logic as your app gets bigger. It's much less of a headache if you just redirected the user's client to another route. Let me know if that doesn't makes sense.

Kenneth Kim
Kenneth Kim
3,795 Points

Hi Mikis :) So if my understanding is correct. We use render to render and view a .pug file; on the other hand, we use redirect to transfer the control to a different route located within the same app.js file. Correct? :)

That's correct, Kenneth :)

Kenneth Kim
Kenneth Kim
3,795 Points

Hi Mikis :) Thanks for answering :D My next question is if lets say the route has a GET method, the system will only allow any redirects to other routes with a GET method only and vice versa for the POST?

Sort of... but it's a bit more complicated than that. Ask this as a new question on the forum and tag me; when I get a chance, I'll answer it.

If you just want something to Google instead, try researching the 307 HTTP Status Code. Also, ExpressJS's redirect method has an optional first-parameter where you can specify a status-code.