Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

javascript - How to send flash messages in Express 4.0?

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 them an error message. I'm trying to do this using this code on the html side:

 <% if (message.length > 0) { %>
   <div class="alert alert-danger"><%= message %></div>
 <% } %>

And doing this in my routes:

router.get('/signup', function(req, res) {
  res.render('/signup', { message: req.flash('signupMessage') });
});

I've tried setting up the config with something along the lines of:

app.configure(function() {
 app.use(express.session({ secret : 'keyboard cat' })); 
});

But this gives me a TypeError:

 12:11:38 web.1  | app.configure(function() {
 12:11:38 web.1  |     ^
 12:11:38 web.1  | TypeError: Object function (req, res, next) {

I'm really confused, because I know I need sessions to be working for flash to work, but sessions don't seem to be working for me. I've also tried using only sessions and no flash, by using req.session.messages, but since I don't have sessions working this obviously did not work.

Any insights? I'm using Express 4.0.0 Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This Gist should answer your question:

https://gist.github.com/raddeus/11061808

in your application setup file:

app.use(flash());

Put that right after you set up your session and cookie parser. That's really all you should need to use flash.

You are using:

req.flash('signupMessage', anyValue);

before redirecting to /signup right?

Here's a fun little tidbit that I currently use for a personal site(in my main application file):

app.use(function(req, res, next){
    res.locals.success_messages = req.flash('success_messages');
    res.locals.error_messages = req.flash('error_messages');
    next();
});

Now every view will have access to any error or success messages that you flash. Works well for me.

One final thing (this is nitpicky but you may gain some knowledge). If you change:

<% if (message.length > 0) { %>

to:

<% if (message) { %>

It will work the same way but will not fail if message is undefined. undefined and empty strings are both considered "falsy" values in javascript.

EDIT: My cookie/session/flash setup goes as follows:

app.use(cookieParser('secretString'));
app.use(session({cookie: { maxAge: 60000 }}));
app.use(flash());

Maybe seeing your application setup code would help. Also note that using app.configure is no longer necessary in Express 4.

Final edit: https://gist.github.com/raddeus/11061808

That is a working example. Go to localhost:3000 after running that app and you should see ['it worked'] on your screen.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...