本文介绍了Connect Flash在Pug中不显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有多个路由的应用程序,并且我想根据用户的交互发送不同类型的Flash消息:成功或失败消息,或者在某些情况下没有消息.目前,消息未显示,我不知道如何使它工作.我正在使用Node,Express和Pug.

I have an application with several routes, and I want to send flash messages of different kinds given the user interaction: either a success or failure message, or in some cases no message. Right now the messages are not displaying and I can't figure out how to get it to work. I'm using Node, Express and Pug.

我有一个server.js文件,routes.js文件,message.pug文件和layout.pug文件.这是我的文件:

I have a server.js file, routes.js file, message.pug file, and layout.pug file. Here are my files:

server.js

server.js

// init project
const express = require('express');
const app = express();
const bodyparser = require("body-parser");
const flash = require('connect-flash');
const passport = require("passport");
const session = require("express-session");

// http://expressjs.com/en/starter/static-files.html
app.use(express.static("public"));
app.set('view engine', 'pug');

// bodyparser middleware
app.use(bodyparser.urlencoded({ extended: false }));
app.use(bodyparser.json());

// express-session middleware
app.use(session({
  secret: process.env.SECRET,
  resave: true,
  saveUninitialized: true
}));

// express-messages middleware
app.use(flash());
app.use((req, res, next) => {
  res.locals.messages = require('express-messages')(req, res);
  next();
});

app.use(express.json());

// import passport-config file
require("./passport-config")(passport);

// passport middleware
app.use(passport.initialize());
app.use(passport.session());

const routes = require('./routes.js');
routes(app);

// listen for requests :)
const listener = app.listen(process.env.PORT, () => {
  console.log('Your app is listening on port ' + listener.address().port);
});

routes.js

routes.js

app.get('/', (req, res) => {
  req.flash("success", "your flash messages are working");
  res.redirect("/admin");
});


app.get("/admin", (req, res) => {
    res.render(process.cwd() + '/views/pug/admin');
  });

message.pug

message.pug

.messages
  each type in Object.keys(messages)
    each message in messages[type]
      div(class="alert alert-" + type) #{ message }
      // expected output
      // div(class="alert alert-success") your flash messages are working

layout.pug

layout.pug

div.col-10.ml-sm-auto.px-4
  != messages('message', locals)

推荐答案

由于我仅发送两种类型的消息(成功或错误),因此我摆脱了messages.pug文件,并使用了以下代码,哪个有用.

Since I had only two types of messages I was sending -- success or error -- I got rid of my messages.pug file and used the following code, which works.

server.js

server.js

// init project
const express = require('express');
const app = express();
const bodyparser = require("body-parser");
const flash = require('connect-flash');
const passport = require("passport");
const session = require("express-session");


// http://expressjs.com/en/starter/static-files.html
app.use(express.static("public"));
app.set('view engine', 'pug');

// bodyparser middleware
app.use(bodyparser.urlencoded({ extended: false }));
app.use(bodyparser.json());

// express-session middleware
app.use(session({
  secret: process.env.SECRET,
  resave: true,
  saveUninitialized: true
}));

// express-messages middleware THIS IS WHAT I CHANGED
app.use(flash());
app.use((req, res, next) => {
  res.locals.errors = req.flash("error");
  res.locals.successes = req.flash("success");
  next();
});

app.use(express.json());

// import passport-config file
require("./passport-config")(passport);

// passport middleware
app.use(passport.initialize());
app.use(passport.session());

const routes = require('./routes.js');
routes(app);

// listen for requests :)
const listener = app.listen(process.env.PORT, () => {
  console.log('Your app is listening on port ' + listener.address().port);
});

routes.js(与以前相同)

routes.js (the same as before)

app.get('/', (req, res) => {
  req.flash("success", "your flash messages are working");
  res.redirect("/admin");
});


app.get("/admin", (req, res) => {
    res.render(process.cwd() + '/views/pug/admin');
  });

然后在布局中,我使用以下内容:

Then in layout, I use the following:

div.col-10.ml-sm-auto.px-4
    if successes
      for success in successes
        div.alert.alert-success #{ success }
    if errors
      for error, i in errors
        div.alert.alert-danger #{ error }

这适用于在重定向中显示消息.

This works for displaying a message in a redirect.

注意:如果要直接显示消息(通过res.render()),则必须像下面这样直接将其传递给render方法:

Note: If you want to display a message directly (via res.render()), you have to pass it to the render method directly like this:

app.get('/admin', (req, res) => {
  req.flash("success", "flash message for a render method");
  res.render("/admin", { successes: req.flash("success") });
});

这篇关于Connect Flash在Pug中不显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 21:15