Categories
JavaScript Answers

How to send flash messages in Express and JavaScript?

Spread the love

Sometimes, we want to send flash messages in Express and JavaScript.

In this article, we’ll look at how to send flash messages in Express and JavaScript.

How to send flash messages in Express and JavaScript?

To send flash messages in Express and JavaScript, we use the connect-flash package.

To install it, we run

npm i connect-flash

Then we use it by writing

const express = require("express");
const flash = require("connect-flash");

const app = express();

app.use(flash());

//...

app.all("/", (req, res) => {
  req.flash("test", "it worked");
  res.redirect("/test");
});

We add the flash middleware with app.use.

Then we send our flash message with req.flash.

Conclusion

To send flash messages in Express and JavaScript, we use the connect-flash package.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *