无法显示快递消息

时间:2017-11-01 13:10:32

标签: javascript node.js express flash-message

所以我正在尝试在 Express 中学习新内容,此时我希望能够显示快速消息。我添加了flash和express-messages作为依赖项,并将消息添加为locals变量

(require('express-messages')(req, res)). 

在EJS中,我添加了<% messages() %>,根据教程,这应该输出带有消息的div元素,但不幸的是,在我尝试触发后,在登录后,它没有显示任何内容。服务器端 index.js 文件中的req.flash('info','test')

谁能看到我做错了什么?

这是我的代码:

app.js:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongodb = require('mongodb');
var session = require('express-session');
var expressValidator = require('express-validator');

var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
var configAuth = require('./auth');
var User = require('./models/Users');
var LocalStrategy = require('passport-local').Strategy;
var bcryptjs = require('bcryptjs');

var flash = require('connect-flash');
var mongoose = require("mongoose");
mongoose.connect('mongodb://localhost/nodeTestProject', function () {
    console.log('Now connected to database nodeTestProject');
});

var index = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(session({
    secret: 'secret',
    saveUninitialized: true,
    resave: true
}));

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


app.use(expressValidator({
    errorFormatter: function (param, msg, value) {
        var namespace = param.split('')
            , root  = namespace.shift()
            , formParam = root;

        while(namespace.length) {
            formParam += '[' + namespace.shift() + ']';
        }
        return {
            param: formParam,
            msg: msg,
            value: value
        };
    }
}));

app.use(flash());

app.listen('5000', function () {
    console.log('Now listening for requests on port 5000 ');
});

app.get('*', function(req, res, next){
    res.locals.user = req.user || null;
    next();
});

app.use(function(req,res,next) {
    res.locals.messages = require('express-messages')(req, res);
    next();
});

app.use('/', index);
app.use('/users', users);

EJS:

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>

    <% if (!user) { %>
    <div class="main row">
    <button class="registerButton">Register</button>
      <button class="loginButton">Log in</button>
      <button class="facebookLoginButton">Facebook login</button>


    <div class="registerDiv">
      <div class="registrationErrors">
        <ul>

        </ul>
      </div>
      <div class="loginDiv">

      </div>

      <% }
      else {
      %>

      <%- messages() %>

      <h1> Welcome <%= user.facebook.name %></h1>
      <img src="<%=user.facebook.photo%>" height="200" width="200" alt="fail">
      <button class="addData">add data</button>
      <div class="addExtraInfo">
            <% if (user.generalInfo) {%>
                <ul>
                    <%
                    for ( prop in user.generalInfo) {
                        if (user.generalInfo.hasOwnProperty(prop)) {%>
                        <li><%= prop %>: <%=user.generalInfo[prop]%></li>
                        <% }} %>

                </ul>
          <% } %>
      </div>
      <br>
      <button class="logoutButton"> Log out</button>

      <% } %>

    </div>
    </div>

    <script src="/jquery-3.2.1.min.js"></script>
    <script src="/javascripts/indexClient.js"></script>
  </body>
</html>

INDEX.JS服务器端:

var express = require('express');
var mongoose = require('mongoose');
var db = mongoose.connection;
var Schema = mongoose.Schema;
var passport = require('passport');
var localStrategy = require('passport-local').Strategy;

var User = require('../models/Users');
var router = express.Router();

router.post('/login/data', passport.authenticate('local', {successRedirect: '/', failureRedirect: '/login'}), function(req, res) {

    req.flash('info', "test");
    res.redirect('/');

});

1 个答案:

答案 0 :(得分:1)

您不应在护照验证回拨中拨打req.flash。您应该返回一条消息as shown here

app.post('/login',
  passport.authenticate('local', { 
    successRedirect: '/',
    failureRedirect: '/login',
    failureFlash: true  // Here
  })
);

Passport会在failureFlash: true时将消息返回到Flash消息。您的护照本地策略应该看起来像这样。

passport.use('local', new LocalStrategy({...},
 function(email, password, done) { 
    User.findOne({ 'local.email' :  email }, function(err, user) {
        if (err)
            return done(err);
        if (!user)
            return done(null, false, {message: 'Oops! No account found.'});
        if (!user.validPassword(password))
            return done(null, false, {message: 'Oops! Wrong password.'});
        return done(null, user);
    });

}));