0

I just started to learn Node JS. While rendering the ejs file, I got an Unexpected token error. Anyone help me out for this error. My code is below:

ERROR:

SyntaxError: Unexpected token { in C:\Users\Ghulam Abbas\Desktop\Node\Conditionals\views\blogpost.ejs while compiling ejs

If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
Or, if you meant to create an async function, pass async: true as an option.
at new Function (<anonymous>)
at Template.compile (C:\Users\Ghulam Abbas\Desktop\Node\Conditionals\node_modules\ejs\lib\ejs.js:633:12)
at Object.compile (C:\Users\Ghulam Abbas\Desktop\Node\Conditionals\node_modules\ejs\lib\ejs.js:392:16)
at handleCache (C:\Users\Ghulam Abbas\Desktop\Node\Conditionals\node_modules\ejs\lib\ejs.js:215:18)
at tryHandleCache (C:\Users\Ghulam Abbas\Desktop\Node\Conditionals\node_modules\ejs\lib\ejs.js:254:16)
at View.exports.renderFile [as engine] (C:\Users\Ghulam Abbas\Desktop\Node\Conditionals\node_modules\ejs\lib\ejs.js:485:10)
at View.render (C:\Users\Ghulam Abbas\Desktop\Node\Conditionals\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\Ghulam Abbas\Desktop\Node\Conditionals\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\Ghulam Abbas\Desktop\Node\Conditionals\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\Ghulam Abbas\Desktop\Node\Conditionals\node_modules\express\lib\response.js:1012:7) 

app.js

var express = require('express');
var app = express();

app.get('/', function(req, res) {
    res.send('Blog Homepage.');
});

app.get('/posts', function(req, res) {

    res.render('blogpost.ejs', {posts : "posts"});

});


app.listen(3000, function() {
    console.log("Server is started.");
});

blogpost.ejs

<h1>Blog <%= posts %> </h1>

Thanks in advance.

Jakki
  • 37
  • 1
  • 6

2 Answers2

2

I just update your code, seem you're forgot set view engine

var express = require('express');
var app = express();
var path = require('path');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.get('/', function(req, res) {
    res.send('Blog Homepage.');
});

app.get('/posts', function(req, res) {

    res.render('blogpost.ejs', {posts : "posts"});

});


app.listen(3000, function() {
    console.log("Server is started.");
});

Please make sure you run npm i ejs. And you may need store all your view files in a folder for more clearly folder struct. In this code, please move your blogpost.ejs to views folder. Hope this helpfull

Truong Dang
  • 3,119
  • 1
  • 15
  • 21
0

const express = require('express');
const path = require('path');
const app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.get('/', (req, res)=>{
  res.render('homepage');
});

app.get('blog', (req, res)=>{
  let post = req.query.post;
  res.render('blog', {post: post});
});
let port = 4444;
app.listen(port, ()=>{
  console.log(`server has started on port: ${port}`);
});