title: TypeError: req.flash is not a function (NodeJs)
body: <p>Working with Registration in a Site. For the register Form,Validation is done using mongoose models and trying to Use <strong>Flash</strong> to display the error message in the Form. </p>
  
  <p>But struct at this error </p>
  
  <blockquote>
    <p>TypeError: res.flash is not a function
        at /home/work/first-node-app/firstnode/routes/index.js:35:11</p>
  </blockquote>
  
  <p>My Code snippets Follows below</p>
  
  <blockquote>
    <p>App.js</p>
  </blockquote>
  
  <pre><code>var createError = require('http-errors');
  var express = require('express');
  var path = require('path');
  var cookieParser = require('cookie-parser');
  var logger = require('morgan');
  var mongoose = require('mongoose');
  var passport = require('passport');
  var session = require('express-session');
  var flash        = require('connect-flash');
  
  var indexRouter = require('./routes/index');
  var commonKeys = require('./config/config');
  
  
  var app = express();
  
  //connect mongoDb 
  mongoose.connect('mongodb://localhost:27017/db', { useNewUrlParser: true });
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'pug');
  
  app.use(logger('dev'));
  app.use(express.json());
  app.use(express.urlencoded({ extended: false }));
  app.use(cookieParser());
  app.use(express.static(path.join(__dirname, 'public')));
  
  // required for passport session
  app.use(session({
    secret: 'secrettexthere',
    saveUninitialized: true,
    resave: true,
    // using store session on MongoDB using express-session + connect  
  }));
  app.use(flash());
  app.use(passport.initialize());
  app.use(passport.session());
  
  
  app.use('/', indexRouter);
  
  module.exports = app;
  </code></pre>
  
  <blockquote>
    <p>Index.js</p>
  </blockquote>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var passport = require('passport');
  var User = require('../models/user-model');
  var Local = require('../config/passport-local');
  
  router.get('/register', function(req, res){
    res.render('public/register',{title:'Register'});
  });
  
  
  router.post('/register', function(req,res,next){
    User.create({name:req.body.name, password:req.body.password, email : req.body.email}, function(err, user){
      if (err) {      
        console.log('Error Inserting New Data');
  
        if (err.name == 'ValidationError') {
            for (field in err.errors) {
              res.flash(field, err.errors[field].message); 
            }
            res.render('public/register');
        }
      }
      if(user) {
        res.send('user registered');
      }    
    })    
  });
  
  module.exports = router;
  </code></pre>
  
  <p>Can anyone point out the error in this code snippets, as am new to Nodejs and Trying to work out the Form validation. <strong>The validation Works fine and the console displays the error but i can't pass that using flash. Can anyone Help me?</strong> <em>And its much appreciated if you confirm my approach to code Nodejs is in correct way</em>. </p>