Express错误地呈现HTML

时间:2019-02-09 23:19:07

标签: javascript express

How do I use HTML as the view engine in Express?在研究了链接之后,我使用端口8001处的index.html文件启动了服务器。但是在控制台中,它不能正确显示:

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

我在html中使用了引起该错误的一些handlebars.js表示法。我的HTML如下:

<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>

当我非常需要这些handlebars.js表达式时,如何仍能克服此错误?

1 个答案:

答案 0 :(得分:0)

阅读official documentation

我找到了这样的代码:

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

var app = express();

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

这意味着我们可以将extname.hbs更改为.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

别忘了安装软件包:

npm i --save express-handlebars