简体   繁体   English

如何使用express和connect-flash显示没有页面刷新的flash消息?

[英]How do I display flash message without page refresh using express and connect-flash?

Express connect-flash displays only after refresh. 快速连接闪存仅在刷新后显示。 Code is stripped for easy reading. 代码被剥离以便于阅读。

"express": "~4.2.0",
"connect-flash": "~0.1.1",

And here is my app.js 这是我的app.js

var express = require('express'),
    favicon = require('serve-favicon'),
    flash = require('connect-flash');
var app = express();
app.use(cookieParser('---'));
app.use(session({
  secret: '---',
  saveUninitialized: true,
  resave: true}));
app.use(flash());

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

In my route i tried something like 在我的路线我试过像

req.flash('success', {msg: 'Sign Up Success'});
res.redirect('/me/dashboard');

and my error display msg.jade template is 我的错误显示msg.jade模板是

for msg in messages
      div #{msg.msg}

I am getting the message only after refresh/redirtect. 我只是在刷新/ redirtect之后收到消息。 Please let me know what i am doing wrong. 请让我知道我做错了什么。

I seen this as well but its outdated i think 我也看到了这一点 ,但我觉得它过时了

You are calling flash method in middleware and this is the expected behaviour of middleware. 您在中间件中调用flash方法,这是中间件的预期行为。 Middleware will read the message on next request & set it local variables according to your logic. 中间件将在下一个请求中读取消息,并根据您的逻辑设置局部变量。

How middle ware works? 中间件如何工作?

When a request comes in, it is passed off to the first middleware function, along with a wrapped ServerResponse object and a next callback. 当请求进入时,它会被传递给第一个中间件函数,以及一个包装的ServerResponse对象和下一个回调。 Middle ware can decide to respond by calling methods on the response object, and/or pass the request off to the next layer in the stack by calling method next(). 中间件可以决定通过调用响应对象上的方法来响应,和/或通过调用方法next()将请求传递给堆栈中的下一层。

connect-flash 连接闪存

The flash is a special area of the session used for storing messages. 闪存是用于存储消息的会话的特殊区域。 Messages are written to the flash and cleared after being displayed to the user. 消息被写入闪存并在显示给用户后被清除。 The flash is typically used in combination with redirects, ensuring that the message is available to the next page that is to be rendered. 闪存通常与重定向结合使用,确保消息可用于要呈现的下一页。

I am getting the message only after refresh/redirect? 我只在刷新/重定向后收到消息?

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

Above middleware will call on every request & set the req.flash() value. 上面的中间件将调用每个请求并设置req.flash()值。 Because of this you will get the req.flash('success', {msg: 'Sign Up Success'}); 因此,您将获得req.flash('success', {msg: 'Sign Up Success'}); values on every next(subsequent) request.Not on the current page without redirecting/refreshing the page. 每个下一个(后续)请求的值。不在当前页面上,不重定向/刷新页面。

To override this behaviour to get the values without refresh you have call the req.flash() function that can be used for flash messages in your router with res.locals.messages = req.flash(); 要覆盖此行为以获取没有刷新的值,您可以使用res.locals.messages = req.flash();调用req.flash()函数,该函数可用于路由器中的flash消息res.locals.messages = req.flash(); .

Ex:- To Show failed login message on the page without page refresh and on success redirect page after setting new flash message in router. 例如: - 在路由器中设置新的Flash消息后,在没有页面刷新的页面上显示失败的登录消息,并在成功重定向页面上显示。

router.post("/login", function (req, res) {
    var username = req.body.username;
    var pwd = req.body.pwd;
    if (username === "demo" && pwd === "demo") {
        req.flash("messages", { "success" : "Sign Up Success" });
        res.redirect("/me/dashboard");
    } else {
        req.flash("messages", { "error" : "Invalid username or password" });
        res.locals.messages = req.flash();
        res.render("login', { title: 'Login"});
    }
});

Conclusion: To get the messages on the same page.Override the behaviour of middleware by assigning values in the router itself. 结论:在同一页面上获取消息。通过在路由器本身中分配值来取消中间件的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM