12

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

I have another function, routed to the same URL, but which handles POST requests, which receives the data submitted by the previous form. If the form does not validate, it redirects the user back to the form; otherwise, it does what should be done:

app.post('/register', function (req, res) {
  if (validate(req.registerForm)) return res.redirect('back');
  persistStuff(req.registerForm, function (err, data) {
    // Do error verification etc.
    res.redirect('back')
  });
});

What I want to do is to send a error message to be presented, in the line:

if (validate(req.registerForm)) return res.redirect('back');

To write something like

if (validate(req.registerForm)) return res.render('form', {msg:'invalid'});

is unacceptable because I want to follow the POST-REDIRECT-GET pattern. I could do something like

if (validate(req.registerForm)) return res.redirect('/register?msg=invalid');

but it would hardcode an URL in my code and I'd prefer to avoid it. Is there another way to do it?

1

2 Answers 2

8

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)

Sign up to request clarification or add additional context in comments.

1 Comment

flash doesn't work in node 4.0 or with templates like mustache.
3

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

2 Comments

Thanks for the alternative, but why or when would it be better than using flash notifications?
Oh wow, didn't notice that you had even suggested this yourself on the bottom of your question - don't think there are any obvious benefits really; it's a little simpler to implement and errors are a bit more visible/trackable since they are on the URL but they should accomplish the same thing. If your {do something} is a little more involved than just displaying something it's probably a little unsafe to handle it this way since users can exploit it by typing error messages themselves on the URL - but otherwise just more straightforward

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.