【问题标题】:Why is handlebars not working in Node.js?为什么把手在 Node.js 中不起作用?
【发布时间】:2022-01-05 10:46:16
【问题描述】:

我已经写了一行:

app.engine('hbs',hbs({extanme:'hbs',defaultLayout:'layout',layoutsDir:__dirname+'/views/layout/',partialsDir:__dirname+'/views/partials/'}));

我已经分配了:

var hbs= require('handlebars');

我得到的错误是:

hbs 不是函数

【问题讨论】:

  • 在使用之前先给hbs赋值
  • 分享你的 app.js
  • 如果它对您有所帮助,那么作为答案的支持或接受将在未来对其他人有所帮助

标签: node.js express handlebars.js


【解决方案1】:

试试这样的

npm i hbs // install hbs package

//app.js
const express = require('express')
const path = require('path')
const hbs = require('hbs')
const app = express()

// View Engine Setup
app.set('views', path.join(__dirname))
app.set('view engine', 'hbs')

app.get('/', function(req, res){
    res.render('Home', {
    array: ['One', 'Two', 'Three', 'Four'],
    message: 'Greetings from geekforgeeks'
    })
})

app.listen(8080, function(error){
    if(error) throw error
    console.log("Server created Successfully")
})

【讨论】:

    【解决方案2】:

    在此处引用 NPM express-handlebar 文档后为您的 app.engine:https://www.npmjs.com/package/express-handlebars。您需要将 .engine 添加到您的 hbs 常量中。

    试试这个!

    var hbs= require('handlebars');
    
    app.engine('hbs', hbs.engine('your config stuff here'));
    

    如果您仍然遇到问题。尝试将 express-handlebars (npm install express express-handlebars) 与 express 一起使用,然后像您一样要求它们。然后它应该工作

    【讨论】:

      猜你喜欢
      • 2021-06-19
      • 2014-08-20
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多