0

I am new in node.js I am using MEAN for my project.In my project I have an option to "Subscribe email" at footer. Because of footer I can't use Angular for flash message (I dont have any controller). Using normal node.js I need to display the flash message.

Here is my code server.js

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var session = require('express-session');
var flash           = require('express-flash-notification')
var cookieParser    = require('cookie-parser')



app.use(cookieParser("hash"));   
app.use(session({secret:"hash", cookie: { maxAge: 60000 }, resave: true, saveUninitialized: true }))

app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.use(express.static('public'));

app.use(flash(app));
app.use(express.static(__dirname + '/client'));
app.use('/admin', express.static(__dirname + '/admin'));

app.use('/', express.static(__dirname + '/client', { redirect: false }));



app.post('/subscribe', function (req, res)
 {
    if(req.body.name == 'aathirag@gmail.com') 
    {
        //return console.log(error);
        //res.send(200);
        req.flash('info', 'Thank you', 'home')

    }
    else
    {
        res.send(500);
    }





});




app.listen(3000, function(){
    console.log('server is running on port 3000..');
});

and html file

<!DOCTYPE html>
<html lang="en">

<body>
    <!-- footer -->
<div>
<form action="/subscribe" method="POST">

    <input name="name" id="name" type="text"  placeholder="Enter your email address" /><input type="submit" name="submit" id="submit" class="newsletter-submit" />


</form>
</div>

</body>
</html>

this shows error "No default engine was specified and no extension was provided".

How to fix this and how I can show message in html page. I have tried

{{info}} 

but its not working

Thank you

user1187
  • 2,116
  • 8
  • 41
  • 74
  • Possible duplicate of [Error: No default engine was specified and no extension was provided](http://stackoverflow.com/questions/23595282/error-no-default-engine-was-specified-and-no-extension-was-provided) – Vasan May 05 '17 at 18:29

2 Answers2

0

you are missing the view engine, for example use jade:

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

If you want use a html friendly syntax use instead ejs and save your html file with .ejs extension.

app.engine('html', require('ejs').renderFile); app.set('view engine', 'html');

after that render your flash variables with res.locals variables like in middleware before routing defines:

res.locals.info = req.flash.info;

now this {{info}} variable use directly via res.locals object.

0

I solve it by an ajax request

<form onsubmit="submit()" method="POST">

    <input name="name" id="name" type="text"  placeholder="Enter your email address" />
<input type="submit" name="submit" id="submit" class="newsletter-submit" />


</form>
<label id="results" class="success" style="display: none;"></label>

Ajax calll

function submit()
  {
     var parameters = { search: $('#name').val() };
       $.post( '/subscribe',parameters, function(data)
       {
         $('#results').html(data);
     });
  } 
user1187
  • 2,116
  • 8
  • 41
  • 74