How to Display Flash Messages using connect-flash Module in Node.js ?
Last Updated :
14 Sep, 2021
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:
- An IDE of your choice installed in your system.
- Node.js and NPM installed and configured.
- 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.
Similar Reads
How to use 'Kleur' Module in Node.js ? 'kleur' module is an external NPM module that can manage color properties in JavaScript applications or projects. It allows developers or programmers to manipulate and use colors in their projects. By using kleur, we change the colors of the outputs in the console which make it looks good.Installati
2 min read
How to Create a Simple Server in Node.js that Display Hello World ? We will create a simple server in Node.js that returns Hello World using an express server. Node.js is a powerful JavaScript runtime built on Chrome's V8 engine, commonly used to build scalable network applications. One of the fundamental tasks when learning Node.js is creating a simple server that
2 min read
How to add new functionalities to a module in Node.js ? Node.js is an open-source and cross-platform runtime environment built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. You need to recollect that NodeJS isnât a framework, and itâs not a programming language. In this article, we will discuss how to add new functi
3 min read
How to detect flash is installed or not using JavaScript ? The task is to detect whether the user has installed Adobe Flash player or not with the help of JavaScript. we're going to discuss 2 techniques. Approach: Create a ShockwaveFlash.ShockwaveFlash object.If the instance's value is true, Flash is installed.If any error occurred, Use navigator.mimetypes
2 min read
How to Print a Message to the Error Console Using JavaScript ? This article will show you how to print a message using JavaScript on the error console. There are three methods to display the error console, these are: Table of Content Using console.error() MethodUsing console.warn() MethodUsing console.info() MethodApproach 1: Using console.error() MethodThe con
2 min read
How to Download a File Using Node.js? Downloading files from the internet is a common task in many Node.js applications, whether it's fetching images, videos, documents, or any other type of file. In this article, we'll explore various methods for downloading files using Node.js, ranging from built-in modules to external libraries.Using
3 min read
Generating Errors using HTTP-errors module in Node.js HTTP-errors module is used for generating errors for Node.js applications. It is very easy to use. We can use it with the express, Koa, etc. applications. We will implement this module in an express application. Installation and Setup: First, initialize the application with the package.json file wit
2 min read
How to Create a Chat App Using socket.io in NodeJS? Socket.io is a JavaScript library that enables real-time, bidirectional, event-based communication between the client and server. It works on top of WebSocket but provides additional features like automatic reconnection, broadcasting, and fallback options.What We Are Going to Create?In this article,
5 min read
How to Send Response From Server to Client using Node.js and Express.js ? In web development, sending responses from the server to the client is a fundamental aspect of building interactive and dynamic applications. Express.js, a popular framework for Node.js, simplifies this process, making it easy to send various types of responses such as HTML, JSON, files, and more. T
4 min read
Explain Colors Module in Node.js The colors module is used to style and color the NodeJS console. It is a nice library for better interaction with your node.js project. Generally what we see is the simple text on the terminal but with this module, we can custom style according to our needs and the conventions i.e. we can change the
3 min read