0

I would like to send error messages back to the client without adding them to the url. Here is my attempt:

exports.register = function(req, res) {
    if (req.body.password != req.body.password_repeat) {
        res.locals.err = 'Passwords must match.';
        res.locals.action = 'register';
        res.redirect('/');
        return;
    }
...

exports.index = function(req, res) {
    req.url = '/';
    res.render('index', {
        action: res.locals.action,
        error: res.locals.error,
        redirect: res.locals.redirect
    });
};

So the redirect works fine and exports.index executes. The problem is that res.locals are gone by then. Is this because once I redirect it is considered a new req/res cycle? Any way I can pass this information through redirect without doing something like res.redirect('/?error=error')

pQuestions123
  • 4,471
  • 6
  • 28
  • 59
  • What you are looking for are flash messages, try this question on SO: http://stackoverflow.com/questions/23160743/how-to-send-flash-messages-in-express-4-0 – victorkt Sep 21 '15 at 00:49

1 Answers1

0

You can use flash package from expressjs, but you need to have session middleware to use it. Also, you can use express-flash package from RGBboy but you need to have both cookieParser and session middlewares in this case.

Gökay Gürcan
  • 1,082
  • 1
  • 10
  • 25