I've been struggling for 2 days on this one, googled and stackoverflowed all I could, but I can't work it out.
I'm building a simple node app (+Express + Mongoose) with a login page that redirects to the home page. Here's my server JS code :
app .get('/', (req, res) => { console.log("Here we are : root"); return res.sendfile(__dirname + '/index.html'); }) .get('/login', (req, res) => { console.log("Here we are : '/login'"); return res.sendfile(__dirname + '/login.html'); }) .post('/credentials', (req, res) => { console.log("Here we are : '/credentials'"); // Some Mongoose / DB validations return res.redirect('/'); });
The login page makes a POST request to /credentials
, where posted data is verified. This works. I can see "Here we are : '/credentials'" in the Node console.
Then comes the issue : the res.redirect doesn't work properly. I know that it does reach the '/' route, because :
(Edit) The redirection is in Mongoose's callback function, it's not synchronous (as NodeJS should be). I have just removed Mongoose validation stuff for clarity.
I have tried adding res.end()
, doesn't work
I have tried
req.method = 'get'; res.redirect('/');
and
res.writeHead(302, {location: '/'}); res.end();
Doesn't work
What am I doing wrong? How can I actually leave the '/login' page, redirect the browser to '/' and display the HTML code that it received?
Thanks a million for your help in advance :)
The res. redirect() function redirects to the URL derived from the specified path, with specified status, a integer (positive) which corresponds to an HTTP status code. The default status is “302 Found”.
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. const app = require('express')(); // The `res.
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.
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.
The problem might not lie with the backend, but with the frontend. If you are using AJAX to send the POST request, it is specifically designed to not change your url.
Use window.location.href
after AJAX's request has completed (in the .done()
) to update the URL with the desired path, or use JQuery: $('body').replaceWith(data)
when you receive the HTML back from the request.
If you are using an asynchronous request to backend and then redirecting in backend, it will redirect in backend (i.e. it will create a new get request to that URL), but won't change the URL in front end.
To make it work you need to:
window.location.href = "/url"
<a></a>
)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With