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.

3
  • Can you post complete code of ejs file? Commented Sep 1, 2019 at 6:11
  • I have just typed this one line only. Commented Sep 1, 2019 at 6:14
  • I have just typed this one line only. Commented Sep 1, 2019 at 6:14

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Can you accept my answer for others who looking for this problem ?
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}`);
});

1 Comment

Could you add a few sentences explaining your code and why it answers the OP's question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.