Open In App

How to Display Flash Messages using connect-flash Module in Node.js ?

Improve
Improve
Like Article
Like
Save
Share
Report

Connect-flash module for Node.js allows the developers to send a message whenever a user is redirecting to a specified web-page. For example, whenever, a user successfully logged in to his/her account, a message is flashed(displayed) indicating his/her success in the authentication.
Prerequisites Before starting with the application, you must have the following:

  1. An IDE of your choice installed in your system.
  2. Node.js and NPM installed and configured.
  3. Basic knowledge of Node.js and its modules.

Installation and Setup: First, initialize our application with a package.json file. Then install the dependencies that are required for our application by the following command:

npm install express express-session connect-flash --save 

Here, express is required by the connect-flash library to run. We are using express-session so that a session can be created whenever a message is flashed and the user is redirected to the specified page.

Now, create a file and name it as app.js. You can give any name of your choice. Now, open the app.js file and import the modules by the following code:

javascript




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


Implementation: Now, comes the main part that is implementation. Write the following code in app.js file:

app.js




const express = require('express');
const session = require('express-session');
const flash = require('connect-flash');
  
const app = express();
  
const port = process.env.PORT || 3000;
  
app.use(session({
    secret:'geeksforgeeks',
    saveUninitialized: true,
    resave: true
}));
  
app.use(flash());
  
app.get('/', (req, res) => {
  req.flash('message', 'Success!!');
  res.redirect('/gfg');
});
  
app.get('/gfg', (req, res) => {
    res.send(req.flash('message'));
});
  
app.listen(port, (err) => {
  if (err) console.log(err);
  console.log('Server is up and listening on', port);
});


After importing all the required dependencies, we are defining a port number on which our app will run. Now, we are defining a session-secret by using of which our sensitive information is encrypted. SaveUninitialized prevents the browser from using empty sessions. Now we are calling our connect-flash module by using app.use(flash()).

Now, here comes the main part of our application. We are defining a route / , which will first flash(display) the specified message and then redirects the user to /gfg route. The /gfg will show the specified message on the web-page. And, finally, we made our application to listen to the specified port and if any error is encountered then the error is logged on the console.

Now, run the application by the following command:

node app.js

Output: The browser will directly redirects the user to /gfg route and will display the output as shown below:

Conclusion: Connect-flash module for Node.js is very useful for developers whenever a flash message is to be sent. In this article, we learned about the connect-flash module. We installed and imported the required dependencies in our application. Finally, we implemented our module and seen how it actually works in a browser.



Last Updated : 14 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads