0

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

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

服务器.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);
});

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

消息.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

布局.pug

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

1 回答 1

0

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

服务器.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") });
});
于 2019-03-22T19:47:55.317 回答