Connect flash 不在 Pug 中显示消息

Connect flash not displaying messages in Pug

我有一个包含多个路由的应用程序,我想根据用户交互发送不同类型的即时消息:成功或失败消息,或者在某些情况下没有消息。现在消息没有显示,我不知道如何让它工作。我正在使用 Node、Express 和 Pug。

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

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

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

.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

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

因为我只发送了两种类型的消息——成功或错误——我摆脱了我的 messages.pug 文件并使用了以下有效的代码。

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(同前)

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

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

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 }

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

注意:如果你想直接显示一条消息(通过res.render()),你必须像这样直接将它传递给render方法:

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