1

This is my login page http://localhost:3000. After form submission I want the page should redirect to the same page if any error occurs and finally want to show those messages in the login page.Currently i am able to the show messages in the login page.But the problem is that the url becomes http://localhost:3000/login as the page redirecting to this url after form submit. I want, it should be localhost:3000.

So is it possible in express.js. I have seen that redirect method does not carry any extra params as error or something like this.

Any help would be appreciated.

2
  • Other people will have no way of visiting your page if you are using the localhost as an address to share it. Commented Oct 10, 2016 at 10:14
  • yes it's true. But is that possible to redirect to the same page ignoring localhost. Commented Oct 10, 2016 at 10:22

1 Answer 1

1

Simply redirect the request from login to '/' as:

app.get('/login',function(req,res){
 //login validation and error messages
 res.redirect('/');
});

To display the error messages back to the login page, you have to store the error messages in session, as res.redirect will end the request-response cycle and messages in request will be removed. So, either you manually store in session and set it in res.locals to get the message directly on the login view or you use req-flash module to display flash messages.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks nivesh for your valuable response. I will definitely try and let you know
was it helpful, if yes then plz upvote the solution.
I am looking for a code to prevent page redirect after form submission. for example the submitting route is router.post('/submit') but wish for page to remain at /form after submitting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.