tj / ejs

Embedded JavaScript templates for node
4.47k stars 513 forks source link

after i login from login form, it will get an error <%- body> is not defined. if i removed <%- body> it will not get an error. please help #254

Open soCasi opened 4 years ago

soCasi commented 4 years ago

after i login from login form, it will get an error <%- body> is not defined. if i removed <%- body> it will not get an error but index.ejs doesnt render to layout.ejs... i have:

LOGIN FOLDER views folder index.ejs layout.ejs faves folder animals.ejs foods.ejs

app folder routes.js server.js

`//server.js var createError = require('http-errors'); var express = require('express'); var session = require('express-session'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var morgan = require('morgan'); var app = express();

var path = require('path'); //var expressLayouts = require('express-ejs-layouts'); var expressLayouts=require("express-ejs-layouts");

//var ejsLayouts = require('express-ejs-layouts');

var ejs = require('ejs');

//*** */ app.use(express.static('public')); app.use('/stylesheets', express.static(dirname + 'public/stylesheets')); app.use('/animations', express.static(dirname + 'public/animations')); app.use('/img', express.static(__dirname + 'public/img'))

//app.set('view options', { layout:'layout.ejs' }); //app.use(logger('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use(cookieParser());

var passport = require('passport'); var flash = require('connect-flash'); const { request } = require('http'); const { response } = require('express');

require('./config/passport')(passport);

app.use(morgan('dev')); app.use(cookieParser()); app.use(bodyParser.urlencoded({ extended: true }));

app.use(session({ secret: 'justasecret', resave:true, saveUninitialized: true }));

app.use(passport.initialize()); app.use(passport.session()); app.use(flash());

require('./app/routes.js')(app, passport);

////no preload //app.set('views', './views'); app.set('views', __dirname + '/views'); app.set('view engine', 'ejs'); app.set('view options', { layout:'layout.ejs' }); //app.use('/',router); app.use(expressLayouts); //app.use(ejsLayouts); app.set("layout extractScripts", true)

app.get('/layout', isLoggedIn, function(_req, res, _next) { res.render('layout', {layout: 'layout.ejs', }); //user:req.user}); });

function isLoggedIn(req, res, next){ if(req.isAuthenticated()) return next();

res.redirect('/'); }

app.get('/foods', function(req, res) { res.render('faves/foods', {title: 'Favorite Foods', foods: ['coconut', 'avocado']}); });

app.get('/animals', function(req, res) { res.render('faves/animals', {title: 'Favorite Animals', animals: ['sand crab', 'corny joke dog']}) });

app.listen(3000, () => { console.log('Server is running at port 3000')

}); module.exports = app;`

`//routes.js ///setting first run***** var express = require('express'); var app = express(); var expressLayouts=require("express-ejs-layouts");

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

app.use(expressLayouts);

module.exports = function(app, passport) { app.get('/', function(req, res){ res.render('login.ejs', {message:req.flash('loginMessage')}); });

app.post('/login', passport.authenticate('local-login', { successRedirect: '/layout', failureRedirect: '/', ////// this one been troubleshooting FM failureFlash: true }), function(req, res){ if(req.body.remember){ req.session.cookie.maxAge = 1000 60 3; }else{ req.session.cookie.expires = false; } res.redirect('layout.ejs'); });`

`//layout.ejs

<!DOCTYPE html>

    wow
<%- body %>
` `index.ejs

This is the home page!

`