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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.