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

后端 未结 2 1644
梦毁少年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

    0 讨论(0)
  • 2020-12-30 07:59

    You need to use flash notifications, and it is built into express.

    You'll add a message like so: req.flash("error", "Invalid form...");

    You'll need a dynamic handler to add the messages to your rendered template, or you can check out the ones TJ has made for express. (express-messages)

    0 讨论(0)
提交回复
热议问题