Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change URL after POST using ExpressJS

I use expressJS as my NodeJS server. The user sends me his login info through a POST and after checking the credentials I render a page:

router.post("/login", function (req: Request, res: Response, next) {
   if(credentialsOK){
      res.render('main');
   }
});

The problem is that the URL becomes http://myaddress/login and I would like to remove the /login of the address. I don't want to use redirect as I want to send local variables through the render.

How can I change the URL?

like image 421
ncohen Avatar asked May 25 '16 00:05

ncohen


2 Answers

You can still pass your local variables through res.redirect.

router.post("/login", function (req: Request, res: Response, next) {
   if(credentialsOK){
       req.session.localVar = yourLocalVar;
       res.redirect('/main');
   }
})

Then in main router:

router.get("/main", function (req: Request, res: Response, next) {
    var yourLocalVar = req.session.localVar;
    res.render('main');
})
like image 107
Jack Wang Avatar answered Sep 26 '22 10:09

Jack Wang


You cannot change the URL from the server side, but you can change the URL by using the javascript method window.history.pushState("", "", '/');

<script>
window.history.pushState("", "", '/');
</script>
like image 35
Pavan Vora Avatar answered Sep 24 '22 10:09

Pavan Vora