如何在Express 4.0中发送Flash消息? [英] How to send flash messages in Express 4.0?

查看:140
本文介绍了如何在Express 4.0中发送Flash消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的Web应用程序需要身份验证,我有一个注册页面,如果该用户尝试使用已经在数据库中的电子邮件注册,我想向他们显示一条错误消息。我试图在html端使用这个代码:

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

但这给我一个TypeError:

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) {

我真的很困惑,因为我知道我需要会话才能使Flash工作,似乎在为我工作,我也尝试只使用会话,没有Flash,通过使用req.session.messages,但由于我没有会话工作这显然不起作用。

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.

任何见解?
我使用的是Express 4.0.0
感谢

Any insights? I'm using Express 4.0.0 Thanks

推荐答案

这个Gist应该回答你的问题:

This Gist should answer your question:

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

在您的应用程序设置文件中:

in your application setup file:

app.use(flash());

在您设置会话和cookie解析器之后,将其放在正确的位置。您真的需要使用Flash。

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

您正在使用:

req.flash('signupMessage', anyValue);

在重定向到/注册之前?

before redirecting to /signup right?

这是一个有趣的小tidbit,我目前用于个人网站(在我的主要应用程序文件):

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.

最后一件事(这是nitpicky,但你可能会获得一些知识)。如果您更改:

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

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

to:

<% if (message) { %>

它将以相同的方式工作,但如果消息未定义,则不会失败。未定义和空字符串都被认为是JavaScript中的falsy值。

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.

编辑:我的cookie /会话/闪存设置如下:

My cookie/session/flash setup goes as follows:

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

也许看到您的应用程序设置代码将有所帮助。另请注意,Express 4中不再需要使用app.configure。

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

最终编辑: https://gist.github.com/raddeus/11061808

这是一个工作的例子。运行该应用程序后,请转到本地主机:3000,您应该在屏幕上看到 [它有效]

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

这篇关于如何在Express 4.0中发送Flash消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆