Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

galih56's avatar

How to use express-messages

I'm trying to make flash session with connect-flash in expressjs.I want to display the message in a pug file.But it's not working.I'm watching a video tutorial about this.

https://www.youtube.com/watch?v=rBzCvbA0Dls&list=PLillGF-RfqbYRpji8t4SxUkMxfowG4Kqp&index=8

this video released 2017,can someone help me to find the problem? is it about the lib version or i'm just missing something?

pug file

        .container
            if success //should i use this?
                include message

            //- != messages('message', locals)  or this?
                        
            block content
            br
            hr
            footer
            p Copyright © 2017
        script(src='/bower_components/jquery/dist/jquery.js')  
        script(src='/bower_components/bootstrap/dist/js/bootstrap.js')
        script(src='/js/main.js')

app.js


const flash = require('connect-flash');
const session = require('express-session');

//Express session
app.use(session({
    secret: 'keyboard cat',
    resave: true,
    saveUninitialized: true,
    cookie: { secure: true }
}));

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


//store article
app.post('/article/add', function (req, res) {
    let article = new Article();
    article.title = req.body.title;
    article.body = req.body.body;
    article.author = req.body.author;
    article.save(function (err) {
        if (err) {
            console.log(err);
        } else {
            req.flash('success', 'Article Added');
            res.redirect('/');
        }
    });
});
0 likes
0 replies

Please or to participate in this conversation.