How to send flash messages in Express 4.0?

后端 未结 6 2397
梦谈多话
梦谈多话 2020-11-28 03:26

So my web application requires authentication, and I have a signup page where if the person tries to sign up with an email that is already in the database, I want to show th

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 03:49

    https://gist.github.com/brianmacarthur/a4e3e0093d368aa8e423

    I, too, was initially confused by flash messages in Express 4. The confusion for me arose partly from the distinction between the concept of flash messaging, a temporary message available to a template, and the various implementations of flash messaging, which include express-flash, other modules, and custom middleware.

    Just to expand on the excellent response from Thad Blankenship above, I created a Gist for the beginner that includes two approaches to flash messages--the express-flash module and custom middleware--rendered in jade, ejs, or handlebars.

    The readme includes details about the getter--req.flash(type)--and setter--req.flash(type, message)--methods exposed by express-flash and how they differ from the exploitation of the res.locals and req.session objects exposed by express-session in the custom middleware.

提交回复
热议问题