I'm trying to display an error message with connect-flash but it seems not working. I've tried several ways to do this work but any of that worked. I gotta an web app with a similar config code and worked without any problem.
here is my code:
const express = require('express')
const moment = require('moment')
const morgan = require('morgan')
const bodyParser = require('body-parser')
const db = require('./configs/db')
const flash = require('connect-flash')
const session = require('express-session')
const cookieParser = require('cookie-parser')
const app = express()
app.set('view engine', 'ejs')
app.set('views', 'views')
app.set(db)
app.use(bodyParser.urlencoded({extended:true}))
app.use(bodyParser.json())
app.use(morgan('dev'))
app.use(cookieParser())
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: { secure: true }
}))
app.use(flash());
var sessionFlash = function(req, res, next) {
res.locals.currentUser = req.user;
res.locals.error = req.flash('error');
res.locals.success = req.flash('success');
next();
}
app.use(sessionFlash)
app.get('/', function (req, res) {
req.flash('error', 'Welcome');
res.render('test', {
title: 'Home',
})
});
app.get('/addFlash', function (req, res) {
req.flash('error', 'Flash Message Added');
res.redirect('/');
});
my EJS file:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<% if(error){ %>
<div class="">
<h1><%=error%></h1>
</div>
<% } %>
</body>
</html>
Aucun commentaire:
Enregistrer un commentaire