【问题标题】:Error: can't find 'dust' module when rendering dust templates on node.js错误:在 node.js 上渲染灰尘模板时找不到“灰尘”模块
【发布时间】:2015-03-23 12:17:49
【问题描述】:

我从 express 为我生成的样板代码开始。我需要dustjs-linkedin 并在app.js 中编译一些简单的模板,如下所示:

var dust = require('dustjs-linkedin');...

app.set('view engine', 'dust');...

var compiled = dust.compile("Hello {name}!","intro");
// add into dust.cache manually
dust.loadSource(compiled);
console.log(dust.cache);

dust.render("intro", {name: "Fred"}, function(err, out) {
  if(err){console.log(err)}
  console.log(out);
});

这一切都很好,我在终端中看到了 HTML 输出。只有当我开始收到此错误时,我才尝试从路线中执行相同的操作:

GET / 500 11.607 ms - 904 Error: Cannot find module 'dust'

app.get('/', function(req, res){
  var compiled = dust.compile("Hello {name}!", "intro");
  dust.loadSource(compiled)
  dust.render("intro", {name: "Fred"}, function(err, out) {
    console.log(out);
    res.send(out);
    res.close();
  });
});

这都在app.js 内,只有它在路由之外起作用,但当我将它移到路由回调中时不起作用。有谁知道为什么它找不到“灰尘”?我需要它,它应该在回调中可见,对吧?

感谢您的帮助!

编辑 1 根据下面的评论,某处需要“灰尘”。我没有在我的代码中这样做;我的猜测是 Express 是在幕后做的,因为我的模板有 '.dust' 文件结尾。我刚刚尝试删除我所有的模板(无论如何都没有使用它们),现在我得到了这个错误:

Error: Failed to lookup view "error" in views directory

我只想看到输出:“Hello Fred”

编辑 2:我想我发现了问题所在

Interrobang 发布的所有内容都是正确的。我认为问题在于 express-generator 为我生成的中间件块:

app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

因此,对于每个请求,此中间件都会触发并引发错误。我不是 100% 确定它在做什么,但如果我评论它,一切正常。我现在的第二个问题是,如果我有的话,到底发生了什么?为什么要为所有请求设置它?

【问题讨论】:

  • 我正在使用:dustjs-linkedin@2.6.1 和 express@4.12.3
  • 该错误并不意味着 dust 未定义——这意味着 require 调用失败。代码中某处有东西在调用require('dust')
  • 我的代码中不需要灰尘,但我在 Express 文档中找到了一些内容:By default, Express will require() the engine based on the file extension. For example, if you try to render a “foo.jade” file, Express invokes the following internally, and caches the require() on subsequent calls to increase performance. app.engine('jade', require('jade').__express); 我的视图确实有一个灰尘文件扩展名,也许 Express 需要灰尘?但是话又说回来……甚至还没有尝试渲染灰尘模板。

标签: node.js express dust.js


【解决方案1】:

您正在将view engine 设置为dust,但您还没有向 Express 注册引擎来告诉它如何渲染 Dust——而 Express 默认无法做到这一点。

考虑使用 consolidatehoffmanadaro(以及其他)作为 Express 的 Dust 渲染引擎。

这是一个使用合并的完整示例。我已经测试过了,这可以在我的机器上运行。

var express = require('express'),
    cons = require('consolidate'),
    app = express();

// assign the dust engine to .dust files 
app.engine('dust', cons.dust);

// set .dust as the default extension 
app.set('view engine', 'dust');
app.set('views', __dirname + '/views');

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

app.listen(3000, function () {
  console.log('Visit http://localhost:3000 woo!');
});

或者,只需从您的代码中删除 view engine 行。您没有使用 Express 进行渲染,因此 Express 甚至不需要知道。

这是一个非常简单的示例,没有经过我测试可以工作的任何视图引擎。您的 Dust 模板位于名为 views 的文件夹中。

var fs = require('fs'),
    path = require('path'),
    express = require('express'),

    dust = require('dustjs-helpers');

dust.config.whitespace = true;

dust.onLoad = function(tmpl, cb) {
  fs.readFile(path.join('./views', path.resolve('/', tmpl + '.dust')),
              { encoding: 'utf8' }, cb);
};

var app = express();
app.get('/', function (req, res) {
  dust.stream("hello", { "world": "World!" }).pipe(res);
});

app.listen(3000, function () {
  console.log('Visit http://localhost:3000 woo!');
});

【讨论】:

  • 感谢您的提示。我刚刚尝试删除 view engine 行,但最终出现此错误:Error: No default engine was specified and no extension was provided.。所以我会选择consolidate。我的非常简单的模板现在加载,但我传递给res.render 的对象似乎没有形成模板的上下文。 {@contextDump} 不返回任何内容。有什么我可以尝试的吗?
  • 如果{@contextDump} 没有返回任何内容,你确定你安装了dustjs-helpers 吗?我在我知道有效的答案中添加了一个非常简单的示例,以帮助您进行调试。尝试设置 dust.debugLevel = "DEBUG" 以查看转储到控制台的任何 Dust 问题。
  • 你是对的。 {@contextDump} 不起作用 b/c 我没有安装 dustjs-helpers。安装后,我现在收到更多描述性错误。我正在渲染的模板实际上就是这样简单:foo{name}bar。在此之后:res.render("index", {name: "Fred"});,我在浏览器中看到 foobar。错误是这样的:[DUST:INFO] Cannot find the value for reference [{name}] in template [null]。此外,我将 consolidate 用于我的视图引擎,尽管我很欣赏你的示例,它展示了它在没有任何视图引擎的情况下工作。
  • 我使用在我的机器上运行的完整脚本更新了第一个示例(合并示例)。它按我的预期工作,显示fooInterrobangbar
  • 啊,是的,当我用你的 app.js 替换我的 app.js 时,它确实有效。我一定在某处做错了什么。任何人,感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多