athira appu
athira appu

Reputation: 27

ejs compilation error in node.js express app

I'm trying to include header.ejs and footer.ejs file with my home.ejs file.

A home.ejs file is located in views folder and the others are located in views/partials.

While including the file I get the error:

SyntaxError: Unexpected identifier in D:\wamp64\www\Training\node_pug\views\home.ejs while compiling ejs

I am a beginner in nodejs.

header.ejs

<DOCTYPE! html>
 <html>
     <head>
         <title>
             <link rel="stylesheet" hreff="app.css">
         </title>
     </head>
 <body>

footer.ejs

</body>
</html>

service.js

var express = require('express');
var app = express();
var path = require('path');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: true });
app.use(express.static('public'));

var session = require('express-session');
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

app.use(express.static(path.join(__dirname, 'public')));
const ejsLint = require('ejs-lint');

app.get('/', function(request, response) {
  response.render('login');
});

app.get('/home', function(request, response) {
  response.render('home');
});  

app.post('/auth', urlencodedParser,function(request, response) {
  var username = request.body.username;
  var password = request.body.password;
    if(username == 'admin')
    {       
       //response.redirect('ejs/home');
       response.render('home');    
    }else{
      response.render('login');
    }
  });

var server = app.listen(8080,function(){
  console.log('server is running...');
});

home.ejs

<% include partials/header %>
<h1>welcome</h1>
<% include partials/footer %>

Upvotes: 0

Views: 772

Answers (2)

ic90
ic90

Reputation: 444

From the docs: The recommended way to include a file is as below:

<%- include('partials/header') %>
 <h1>welcome</h1>
<%- include('partials/footer') %>

The docs state that

NOTE: Include preprocessor directives (<% include user/show %>) are not supported in v3.0+.

You are probably using a v3.0+ version of EJS hence the error you are experiencing. The snippet above should solve your issue.

Upvotes: 3

Wejd DAGHFOUS
Wejd DAGHFOUS

Reputation: 1110

If you are using express 4.X you should probably use this syntax in your home.ejs.

<%- include partials/header.ejs %>
 <h1>welcome</h1>
<%- include partials/footer.ejs %>

Upvotes: 0

Related Questions