Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between location and redirect in node.js

What is the use of res.location() method? I can use res.redirect() to redirect to a particular URL and i cannot see any change if i use res.location() before res.redirect()

like image 504
Ashutosh Avatar asked Mar 27 '14 03:03

Ashutosh


People also ask

How do I redirect a path in node JS?

nodejs1min read In express, we can use the res. redirect() method to redirect a user to the different route. The res. redirect() method takes the path as an argument and redirects the user to that specified path.

How do I redirect a website in node?

redirect() function, we can now discuss how to redirect back to original URL in NodeJS. Back redirect: We can use this method to redirects the request back to the referrer. If no referrer is present, the request is redirected to “/” route by default.

How do I redirect a login page in node JS?

user = o; res. redirect('/home'); } else{ res. render('/login', { title: 'Hello - Please Login To Your Account' }); } }); } }); After that, all other html websites are linked from within dashboard.

How does express redirect work?

The res. redirect() function lets you redirect the user to a different URL by sending an HTTP response with status 302. The HTTP client (browser, Axios, etc.) will then "follow" the redirect and send an HTTP request to the new URL as shown below.


1 Answers

They are very similar in their description, but one does much more. The easiest way to see the difference is look at the source.

res.location just sets the response header. It does not set a response status code or close the response, so you can write a response body if you want, and you have to call res.end() on your own after.

res.redirect on the other hand sets the status to 302, sets the header (using res.location) and sends a nice response body saying that the user is being redirected, and renders a link if their browser doesn't automatically redirect them for some reason.

like image 163
loganfsmyth Avatar answered Sep 19 '22 13:09

loganfsmyth