Video Course

Create a CRUD App with Node and MongoDB: Lesson 14 of 17

Showing Success Messages

Up Next

Validation Error Messages

Autoplaying in 7 seconds!

Cancel

In order to save messages (success or error) and display them on the client, since we are creating a round-trip application, we will need to use sessions. We'll need:

  • express-session: Give us the ability to create sessions in Node so that we can store data.
  • cookie-parser: Give us the ability to read those sessions that are stored in cookies.
  • connect-flash: Let us save messages to flash-data (one-time use messages) which we'll display as success/error messages.

Install all three packages with:

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

Then we can configure in server.js:

var session        = require('express-session'),
  cookieParser   = require('cookie-parser'),
  flash          = require('connect-flash');

// set sessions and cookie parser
app.use(cookieParser());
app.use(session({
  secret: process.env.SECRET, 
  cookie: { maxAge: 60000 },
  resave: false,    // forces the session to be saved back to the store
  saveUninitialized: false  // dont save unmodified
}));
app.use(flash());

Chris Sevilleja

163 posts

Co-founder of Scotch.io. Slapping the keyboard until something good happens.