Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Sitemap
Menu

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

Posted on January 16, 2023

 

 

To display flash messages using the connect-flash module in Node.js and Express, you will need to perform the following steps:

 

  1. Install the connect-flash module by running npm install connect-flash in your terminal.

 

 

2. In your Express app, require the connect-flash module and add it as middleware. For example:

 

 

 

JavaScript
1
2
3
const flash = require('connect-flash');
 
app.use(flash());

 

 

  1. In your route handlers, set flash messages using the req.flash function. For example:
JavaScript
1
req.flash('success', 'Welcome to our website!');

 

 

  1. In your views, display the flash messages using the req.flash function. For example, in an EJS template:

 

 

1
2
3
4
5
<% if (success) { %>
    <div class="alert alert-success">
        <%= success %>
    </div>
<% } %>

 

 

  1. Make sure to call req.flash() after each redirect, otherwise the message will be lost.

 

 

6) You can also add different type of flash message like error, success, warning and info.

 

 

Recent Posts

  • Build a Fixed Deposit Interest Calculator Using Amount and Time in Browser Using HTML5 & Javascript
  • How to Download Files From URL in Node.js Using Node-Downloader-Helper Library
  • Angular 10 Image Carousel and Video Gallery Lightbox Modal Slider Using ng-image-slider Library
  • React Unsplash Api Photo Search App
  • React Form Validation Using Formik and Yup
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • javascript
  • Koajs
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme