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

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

问题描述

因此,我的 Web 应用程序需要身份验证,并且我有一个注册页面,如果此人尝试使用数据库中已有的电子邮件进行注册,我想向他们显示错误消息.我正在尝试在 html 端使用此代码执行此操作:

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

在我的路线中这样做:

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

我尝试使用以下内容设置配置:

app.configure(function() {app.use(express.session({秘密:'键盘猫'}));});

但这给了我一个类型错误:

 12:11:38 web.1 |app.configure(function() {12:11:38 web.1 |^12:11:38 web.1 |类型错误:对象函数(req、res、next){

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

有什么见解吗?我正在使用 Express 4.0.0谢谢

解决方案

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

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

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

app.use(flash());

在设置会话和 cookie 解析器后立即执行.这就是使用 Flash 所需的全部内容.

您正在使用:

req.flash('signupMessage', anyValue);

在重定向到/signup 之前对吗?

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

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

现在每个视图都可以访问您闪烁的任何错误或成功消息.对我来说效果很好.

最后一件事(这很挑剔,但您可能会获得一些知识).如果你改变:

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

到:

<% if (message) { %>

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

我的 cookie/session/flash 设置如下:

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

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

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

这是一个工作示例.运行该应用程序后转到 localhost:3000,您应该会在屏幕上看到 ['it working'].

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

解决方案

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.

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

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