我正在尝试将nunjucks模板引擎与expres js一起使用。页面呈现正确,但控制台上出现错误。
Error: No default engine was specified and no extension was provided.
nunjucks文档

var app = express();

nunjucks.configure('views', {
    autoescape: true,
    express: app
});

app.get('/', function(req, res) {
    res.render('index.html');
});

我追踪了错误,发现它来自at new NunjucksView (C:\Users\future\Desktop\New folder (2)\node_modules\nunjucks\src\express-app.js:13:13)
innodemodules/nunjucks/src/express-app.js抛出错误
    if (!this.ext && !this.defaultEngine) {
      throw new Error('No default engine was specified and no extension was provided.');
    }

这意味着,据我所知,DefaultEngine未设置。
Github Repo
如何在使用nunjuck时设置默认模板引擎。

最佳答案

您应该将express的默认view engine设置为与nunjucks已知/呈现的扩展名相同

const express =  require('express');
const nunjucks = require('nunjucks');

const app = express();

// set default express engine and extension
app.engine('html', nunjucks.render);
app.set('view engine', 'html');

// configure nunjucks engine
nunjucks.configure('views', {
    autoescape: true,
    express: app
});

app.get('/', function(req, res) {
    res.render('index');
});

app.listen(9090, () => {
  console.log('http://localhost:9090')
});

如果要更改模板/视图扩展名,可以这样更改:
app.engine('nunj', nunjucks.render);
app.set('view engine', 'nunj');

然后重命名模板/视图index.nunj

09-05 00:56