0

How do I use HTML as the view engine in Express? after studying the link I started server with an index.html file at port 8001. However it doesnot renders correctly saying in console:

Server Started
Error: SyntaxError: Unexpected token !== in file /home/bereznyak/Рабочий стол/restplatzproject/views/index.html.

I use in my html some handlebars.js notations which cause this error. My HTML is as follows:

<div class="item_card">
            <div class="restaurant_name">
            <p>Empfang restaurant: <span class="decorated">{{cellsArray.[0]}}</span></p>
            </div>
            <div class="updated">
              Updated at: {{cellsArray.[1]}}
            </div>
            <!-- этот div скрыт, причины описаны в js.js файле, копия этого diva' в следующем div'e -->
            <div style="display: none" class="places_number">
            <p> <span class=" getme places_number_from_google decorated">{{cellsArray.[2]}}</span> Places.</p>
            </div>
            <div class="places_number">
              <p> <span class="places_number_js"></span> Places.</p>
            </div>
            <div class="discount">
            <p><span class="decorated">{{cellsArray.[3]}} %</span> your Discount</p>
            </div>
            <div class="time">
              <p>Discount valid from <span class="decorated">{{cellsArray.[4]}}</span> till <span class="decorated">{{cellsArray.[5]}}</span> </p>
            </div>
            <div id="button" class="button" onclick="displayDiv()">
                <form action="/pay" method="post">
                    <input type="submit" value="Buy">
                </form>
            </div>
          </div>

How can I still overcome this error as I dramatically need these handlebars.js expressions?

Andy Hoffman
  • 18,436
  • 4
  • 42
  • 61

1 Answers1

0

After reading official documentation

I found such code:

var express = require('express');
var exphbs  = require('express-handlebars');

var app = express();

app.engine('.hbs', exphbs({extname: '.hbs'}));
app.set('view engine', '.hbs');

it means we can change extname from .hbs to .html:

const exphbs  = require('express-handlebars');

app.engine('.html', exphbs({extname: '.html'})); // defining view engine for file extension 
app.set('view engine', '.html'); // defining default extension

don't forget to install package:

npm i --save express-handlebars
num8er
  • 18,604
  • 3
  • 43
  • 57
  • `app.get('/', (req, res) => res.render('index'));` my next line of code which results in: **Error: Failed to lookup view "index" in views directory "/home/bereznyak/Рабочий стол/restplatzproject/views"** What's wrong about it? – Alexey Bereznyak Feb 10 '19 at 00:01