Passing error message to template through redirect in Express/Node.js

后端 未结 2 1643
梦毁少年i
梦毁少年i 2020-12-30 07:09

In my Node.js application, I have a function (routed by Express) which presents a form to the user:

app.get(\'/register\', function (req, res) {
  res.render         


        
2条回答
  •  别那么骄傲
    2020-12-30 07:42

    You could simply have it redirect as res.redirect('..?error=1')

    the ? tag tells the browser that it is a set of optional parameters and the .. is just a pathname relative recall (like calling cd .. on terminal to move back one directory) and you're browser will direct to the appropriate page with that tag at the end: http://.....?error=1

    then you can simply pull the error on the appropriate page by doing a:

    if (req.param("error" == 1)) { // do stuff bassed off that error match };

    you can hardcode in several different error values and have it respond appropriately depending on what error occurred

提交回复
热议问题