我使用expressJS作为我的NodeJS服务器。用户通过POST向我发送他的登录信息,并在检查凭证后呈现页面:
router.post("/login", function (req: Request, res: Response, next) {
if(credentialsOK){
res.render('main');
}
});
问题是网址变为http://myaddress/login,我想删除地址的/ login。我不想使用重定向,因为我想通过渲染发送局部变量。
如何更改网址?
答案 0 :(得分:6)
您仍然可以通过res.redirect
传递本地变量。
router.post("/login", function (req: Request, res: Response, next) {
if(credentialsOK){
req.session.localVar = yourLocalVar;
res.redirect('/main');
}
})
然后在main
路由器:
router.get("/main", function (req: Request, res: Response, next) {
var yourLocalVar = req.session.localVar;
res.render('main');
})
答案 1 :(得分:1)
您无法从服务器端更改URL,但可以更改URL
使用javascript方法window.history.pushState("", "", '/');
<script>
window.history.pushState("", "", '/');
</script>