res.redirect('back') with parameters

后端 未结 7 1997
刺人心
刺人心 2020-12-23 09:44

I have a register form on every page of my website. During registration some error may occur. After catching the error, I have to return the user to the previous page, sho

相关标签:
7条回答
  • 2020-12-23 10:30

    A really easy way of implementing this is to use connect-flash, a middleware for express that leverages the session middleware built into express to pass 'flash' messages to the next request.

    It means you don't need to add fields to track urls or messages to the form or route patterns for your views.

    It provides a simple version of the same feature in Ruby on Rails.

    (obviously you have to be using the Express framework for this to use, but I highly recommend that too!)

    Plug it in like this:

    var flash = require('connect-flash');
    var app = express();
    
    app.configure(function() {
      app.use(express.cookieParser('keyboard cat'));
      app.use(express.session({ cookie: { maxAge: 60000 }}));
      app.use(flash());
    });
    

    And then use it like this:

    app.get('/flash', function(req, res){
      req.flash('info', 'Flashed message')
      res.redirect('/');
    });
    
    app.get('/', function(req, res){
      res.render('index', { message: req.flash('info') });
    });
    
    0 讨论(0)
提交回复
热议问题