Send message on redirect in node js | redirect with message node js | Flash message in node js

Some time we need to print a message after the redirection of page.
Like when you delete a post and will show “Post deleted successfully!” after reloading or redirecting the page.
I’m going to explain how we can redirect to another page in node js along with message.
First you need to install  express-session using below command if you have not installed:

Now you need to install req-flash using below command. It flash the message but this library is dependent on session.

Now in app.js include following:

If you are using express js might be you don’t need to put this line because this is already there.

Now you need to use session if you have not used it in app.js yet:

Now use flash in app.js file:

Now you can put your message in following way :

and you can achieve the message by following code.

so you can render your messages to view like this

Now on view you can print the message like :

Here I’m using ejs template engine.

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *