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?
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');
})
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>
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