title: resolve TypeError: req.flash is not a function?
body: <p>Working with Registration in a Site. For the register Form,Validation is done using mongoose models and trying to Use Flash to display the error message in the Form.</p>
  <p>In my nodejs app it shows an error like:</p>
  <blockquote>
  <p>TypeError: req.flash is not a function</p>
  </blockquote>
  <p>I have installed connection-flash npm it is throwing an error like:</p>
  <blockquote>
  <p>TypeError: req.flash is not a function at</p>
  </blockquote>
  <blockquote>
  <p>E:\node-course\apnadukan\src\routes\index.js:21:24</p>
  </blockquote>
  <pre><code>app.js
  
  const express = require('express');
  const path = require('path');
  const hbs = require('hbs');
  const flash = require(&quot;connect-flash&quot;);
  const passport = require(&quot;passport&quot;);
  const db = require('./connection/db');
  
  // Express Use
  const app = express()
  const port = 3000;
  
  // Form Data Get
  app.use(express.json());
  app.use(express.urlencoded({extended:false}));
  
  // HBS Handlebar use
  app.set('view engine', 'hbs');
  
  // Router Use
  app.use('/', require(path.join(__dirname, 'routes/index.js')))
  // Publics assess link (HBS)
  const static_path = path.join(__dirname, '../publics');
  app.use(express.static(static_path));
  // View Set (HBS)
  const views = path.join(__dirname, '../src/views');
  const partials_path = path.join(__dirname, '../src/views/partials');
  app.set('views', views);
  hbs.registerPartials(partials_path);
  
  // 
  app.use(flash());
  app.use(passport.initialize());
  app.use(passport.session());
  
  
  app.listen(port, () =&gt; {
  console.log(`Example app listening on port ${port}`)
  })
  
  
  
  index.js
  
  const express = require('express');
  const addUser = require('../models/register');
  let passport = require(&quot;passport&quot;);
  let LocalStrategy = require(&quot;passport-local&quot;).Strategy;
  // const passport = require('../config/passport');
  const {validationRegister, validationRegisterMsg} = require('../config/validator');
  
  
  
  
  // const router = express.Router()
  const router = require('express').Router()
  
  router.get('/' , (req , res)=&gt;{
  res.render('index', {
  title: &quot;Welcome To Apna Dukan&quot;
  })
  })
  router.get('/welcome-to-apnadukan', (req , res)=&gt;{
  var errorMsg = req.flash('error')[0];
  res.render('register', {
  title: &quot;Register | Welcome To Apna Dukan&quot;,
  errorMsg,
  page_name: &quot;Register&quot;
  });
  });
  router.post('/welcome-to-apnadukan',
  [
  validationRegister(), 
  validationRegisterMsg,
  passport.authenticate(&quot;local.register&quot;, {
  successRedirect: &quot;/login&quot;,
  failureRedirect: &quot;/welcome-to-apnadukan&quot;,
  failureFlash: true,
  }),
  ], 
  async (req , res)=&gt;{
  try {
  if (req.session.oldUrl) {
  let oldUrl = req.session.oldUrl;
  req.session.oldUrl = null;
  res.redirect(oldUrl);
  } else {
  res.redirect(&quot;/welcome-to-apnadukan&quot;);
  }
      
  } catch (error) {
  req.flash(&quot;error&quot;, error.message);
  res.status(400).send(error);
  return res.redirect(&quot;/&quot;);
  }
  })
  
  router.get('/login' , (req , res)=&gt;{
  res.render('login', {
  title: &quot;Login | Welcome To Apna Dukan&quot;,
  page_name: &quot;Login&quot;
  })
  })
  router.post('/login' , (req , res)=&gt;{
  req.send(&quot;Update&quot;);
  })
  router.get('/recover-password', (req, res)=&gt;{
  res.render('recoverpassword',{
  title: &quot;Recover Password | Welcome To Apna Dukan&quot;,
  page_name: &quot;Recover Password&quot;
  })
  })
  router.post('/recover-password', (req, res)=&gt;{
  res.render('recoverpassword',{
  title: &quot;Recover Password | Welcome To Apna Dukan&quot;,
  page_name: &quot;Recover Password&quot;
  })
  })
  
  router.get('/page' , (req , res)=&gt;{
  res.send('Hello World!')
  })
  
  module.exports  = router
  
  
  Validator.js
  
  
  const { check, validationResult } = require(&quot;express-validator&quot;);
  
  const validationRegister = (()=&gt;{
  return [
  check('name', 'Name is required').not().isEmpty(),
  check('username', 'Username is required').not().isEmpty().matches('/^(?![0-9]*$)[a-zA-Z0-9]+$/').withMessage('Only AlphaNumeric character is allowed').isLength({min:6,max:6}).withMessage('Minimum 6 characters required'),
  check('email', 'Email is required').not().isEmpty().isEmail().withMessage('Email must be a valid email address.'),
  check('password', 'Password is required').not().isEmpty().matches('/^(?=(.*[a-zA-Z].*){2,})(?=.*\d.*)(?=.*\W.*)[a-zA-Z0-9\S]{6,}$/').withMessage('Strong passwords with min 6  characters, at least two letters (not case sensitive), one number, one special character, space is not allowed'),
  ];
  });
  const validationRegisterMsg = (req, res, next)=&gt;{
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
  let messages = [];
  errors.array().forEach((error)=&gt;{
      messages.push(error.msg)
  });
  req.flash('error', messages);
  // req.flash({error: messages})
  return res.redirect('/register');
  }
  next();
  };
  
  module.exports = {validationRegister,validationRegisterMsg};
  
  
  passport.js
  
  const passport = require(&quot;passport&quot;);
  const LocalStrategy = require(&quot;passport-local&quot;).Strategy;
  const User = require(&quot;../models/register&quot;);
  
  passport.serializeUser((user, done) =&gt; {
  done(null, user.id);
  });
  
  passport.deserializeUser((id, done) =&gt; {
  User.findById(id, (err, user) =&gt; {
  done(err, user);
  });
  });
  
  passport.use(
  &quot;local.register&quot;,
  new LocalStrategy(
  {
  nameField: 'name',
  usernameField: 'username',
  emailField: 'email',
  passwordField: 'password',
  passReqToCallBack: true
  },
  async (req, username, email, done) =&gt; {
  try {
  const username = await Customer_users.findOne({ username: username });
  if (username) {
      return done(null, false, { message: &quot;Username already exists&quot; });
  }
  const user = await Customer_users.findOne({ email: email });
  if (user) {
      return done(null, false, { message: &quot;Email already exists&quot; });
  }
  const newUser = await new User();
  newUser.name = name;
  newUser.username = username;
  newUser.email = email;
  newUser.password = newUser.encryptPassword(password);
  await newUser.save();
  return done(null, newUser);
  } catch (error) {
  console.log(error);
  return done(error);
  }
  }
  )
  );
  </code></pre>