My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

express-flash-notification giving Failed to lookup view “flash” in views directory?

Atul kumar's photo
Atul kumar
·Aug 15, 2018

I have an app which is using express-flash-notification to display messages on the website but i'm getting error of failing lookup view

Here is my code in the app :-

app.set('views',__dirname+'/views');

app.set('view engine','ejs');

app.use(bodyParser.urlencoded({extended:false})); app.use(bodyParser.json()); app.use(cookieParser()); app.use(express.static(path.join(__dirname,'public'))); app.use(session({ secret: "cats",saveUninitialized: true, resave: true })); app.use(passport.initialize()); app.use(passport.session()); app.use(flash(app)); app.post('/sendemail',(req, res) => { const output = <p>You have a new contact request</p> <h3>Contact Details</h3> <ul> ; <li>Name: ${req.body.name}</li> <li>Email: ${req.body.email}</li> <li>Phone: ${req.body.phone}</li> </ul> <h3>Message</h3> <p>${req.body.message}</p>;

// create reusable transporter object using the default SMTP transport let transporter = nodemailer.createTransport({service: 'gmail ',

auth: { user: '', pass: 'xxxxxxx' }, tls:{ rejectUnauthorized:false } });

// setup email data with unicode symbols let mailOptions = { from: '"Enquiry from datadock" atul.11192@gmail.com', // sender address to: '', // list of receivers subject: 'Datadock Enquiry ', // Subject line text: 'Hello world?', // plain text body html: output // html body };

// send mail with defined transport object transporter.sendMail(mailOptions, (error, info) => { if (error) { req.flash('message','error occured'); } req.flash('message','your enquiry has been sent','home');

}); });

and this the code in the ejs file (where notification should be shown):-

<div class="col-md-6 col-sm-12 col-xs-12"> <h3 ><u>Send Us An Enquiry</u></h3> <% if(message){%> <div class="alert alert-primary" role="alert"> <%=message%> </div> <%}%> <form action="/sendemail" method="POST"> <div class="form-group"> <input type="email" class="form-control" aria-describedby="email Help" placeholder="Email Id *" name="email"> </div> <div class="form-group"> <input type="text" class="form-control" aria-describedby="name Help" placeholder="Name *" name="name"> </div> <div class="form-group"> <input type="phone" class="form-control" aria-describedby="Phone Help" placeholder="Contact no. *" name="phone"> </div> <div class="form-group"> <textarea type="message" class="form-control" rows="3" placeholder="Write your enquiry *" name="message"></textarea> </div> <div class="form-group text-center"> <button type="sumbit" class="btn btn-primary">Send</button> </div> </form> </div> </div> </div>

and i'm getting the following error :-

Error: Failed to lookup view "flash" in views directory

can anyone tell me where iam going wrong and how i can improve it to see the optimum result?