The connect-flash module in NodeJS allows developers to render a pop-up message whenever a user is redirected to a particular webpage. For example, in your Nodejs demo application, you want to notify your users on logging in and logging out. For that, you can use flash messages in NodeJs app with the help of the connect-flash module.
Before getting started with the development of our demo app let’s see what are the prerequisites-
The first step would be creating package.json to initialize the project. For that, run the below command
To implement flash messages in NodeJs with connect-flash module, you need to install the required dependencies using the command.
Express: Required by the library connect-flash to run.
Express-session: to create a session whenever a message is flashed, so that the user can be redirected to a particular page.
Now, heading towards the coding part. Create a file named index.js. You can name it anything you want. Now, open the index.js file and import modules using the below code.
const express = require('express'); const session = require('express-session'); const flash = require('connect-flash'); const app = express();
Now, it’s time to get our hands on the logical part and implement it. Write the code in index.js file as shown below.
const express = require('express'); const session = require('express-session'); const flash = require('connect-flash'); const app = express(); const port = process.env.PORT || 3001; app.use(session({ secret:'flashblog', saveUninitialized: true, resave: true })); app.use(flash()); app.get('/', (req, res) => { req.flash('message', 'Welcome to Blog'); res.redirect('/display-message'); }); app.get('/display-message', (req, res) => { res.send(req.flash('message')); }); app.listen(port, (err) => { console.log('Server is up and listening on', port); });
Explanation
Later define routes:
Voila! We made our demo app to listen to the port.
Are you seeking to elevate your Node.js app’s user experience?
Look no further! As a leading Node.js development company, we specialize in crafting expert Flash Messages tailored to captivate your users. Get in touch today!
Once done with the development part. Run the application by the following command and test the functionality.
The browser will be redirected to the /display-message and it will show the following output.
Now we will display flash messages on the view file. For this, we need to use the ejs view engine. To install ejs run the below command.
Open index.js and add below code
app.set('view engine', 'ejs'); app.use(function(req, res, next){ res.locals.message = req.flash(); next(); });
Create views folder on root of the application and create display.ejs in the same folder. Now render the view template (display.ejs) in display-message route using the below code.
Once done, add the below line in views/index.js.
To beautify the flash message we will use bootstrap css. Include bootstrap css file into view template and use the below code.
Restart the node server and open http://localhost:3001/ in the browser.
The output will look something like this.
Feel free to visit the source code: flash-messages-in-nodejs. You can clone the github repository and play around with the code as well.
I hope the quick guide was helpful to implement Flash messages in NodeJS using the connect-flash module. Are you a NodeJS enthusiast? If yes, then there’s an opportunity for you! Visit the NodeJS tutorials page and learn the basics and advanced of NodeJS.
Bacancy has dedicated and skilled developers who thoroughly go through the client’s requirements to meet the optimum solutions. In case, you are looking for a NodeJS developer who has sharp problem-solving instincts then contact us and hire Node developer.
Your Success Is Guaranteed !
We accelerate the release of digital product and guaranteed their success
We Use Slack, Jira & GitHub for Accurate Deployment and Effective Communication.