Variable not defined ejs

Video Variable not defined ejs

my login form is ok, my layout.ejs without tag <%- body> inside is working. but when i put <%- body> purposely to render index.ejs with somethingtext… it will get an error <%- body> is not defined.

I’m using node.js, javascript and express ejs layouts. index.ejs cannot render to if i put <%- body> to layout.ejs it will get result <%- body > is not defined… please help fix this… when i login i gets an error “body is not defined”. But when i removed <%- body> it will not get an error but the index.ejs something doesnt render to layout.ejs…

index.ejs <h1>This is the home page!</h1> layout.ejs <!DOCTYPE html> <html lang=”en”> <head> <meta charset=”UTF-8″> <meta name=”viewport” content=”width=device-width, initial-scale=1.0, shrink-to-fit=no”> <meta http-equiv=”X-UA-Compatible” content=”ie=edge”> </head> <div> <ul>wow</ul> <%- body %> </div> <body> </body> </html> //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’)}); });‘/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’); }); //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;

Related Posts