使用ExpressJS在POST之后更改URL [英] Change URL after POST using ExpressJS

查看:157
本文介绍了使用ExpressJS在POST之后更改URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将expressJS用作NodeJS服务器.用户通过POST向我发送他的登录信息,并在检查了凭据后呈现了一个页面:

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

问题是该URL变为 http://myaddress/login ,我想删除以下网址的/login地址.我不想使用重定向,因为我想通过渲染器发送局部变量.

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.

如何更改URL?

推荐答案

您仍然可以通过res.redirect传递局部变量.

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

然后在main路由器中:

router.get("/main", function (req: Request, res: Response, next) {
    var yourLocalVar = req.session.localVar;
    res.render('main');
})

这篇关于使用ExpressJS在POST之后更改URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆