dustjs-linkedin with express 3

时间:2014-01-19 13:01:16

标签: node.js express template-engine dust.js

我想让dustjs-linkedin使用快速3项目,但我似乎无法克服这个错误:

Error: Template name parameter cannot be undefined when calling dust.compile
at Object.compiler.compile (/home/user/project/node_modules/dustjs-linkedin/lib/compiler.js:21:16)
at Object.dust.compileFn (/home/user/project/node_modules/dustjs-linkedin/lib/dust.js:109:37)
at Function.exports.dust.render (/home/user/project/node_modules/consolidate/lib/consolidate.js:226:56)
at /home/user/project/node_modules/consolidate/lib/consolidate.js:146:25
at /home/user/project/node_modules/consolidate/lib/consolidate.js:99:5
at fs.js:266:14
at Object.oncomplete (fs.js:107:15)

我可以用一个全新的快递3项目来重现这个:

app.js

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

var app = express();

// added these 3
var dust = require('dustjs-linkedin');
var cons = require('consolidate');
app.engine('dust', cons.dust);


app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));

// added this 1
app.set('view engine', 'dust');

app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

package.js

{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.4.7",
    "dustjs-linkedin": "*",
    "consolidate": "*"
  }
}

路由/ index.js

exports.index = function(req, res){
  res.render('index', { title: 'Express' });

视图/ index.dust

Title: {title}

奇怪的是,我有一个使用快递3和dustjs-linkedin的项目工作得很好。我在dustjs-linkedin模块中的文件上运行了差异,工作版本在dist中有很多文件,而抛出错误的项目只有一些,即:

工作 / node_modules / dustjs-LinkedIn / DIST:

dust-core.js  dust-core.min.js  dust-full.js  dust-full.min.js  LICENSE

workingProject / node_modules / dustjs-LinkedIn / DIST:

dust-core-1.1.1.js      dust-core-2.0.0.min.js  dust-full-1.2.0.js      dust-full-2.0.1.js
dust-core-1.2.0.js      dust-core-2.0.1.js      dust-full-1.2.1.js      dust-full-2.0.2.js
dust-core-1.2.1.js      dust-core-2.0.2.js      dust-full-1.2.2.js      dust-full-2.0.3.js
dust-core-1.2.2.js      dust-core-2.0.3.js      dust-full-1.2.2.min.js  dust-full-2.1.0.js
dust-core-1.2.2.min.js  dust-core-2.1.0.js      dust-full-1.2.3.js      dust-full-2.2.0.js
dust-core-1.2.3.js      dust-core-2.2.0.js      dust-full-1.2.3.min.js  dust-full-2.2.2.js
dust-core-1.2.3.min.js  dust-core-2.2.2.js      dust-full-1.2.4.js      dust-full-2.2.2.min.js
dust-core-1.2.4.js      dust-core-2.2.2.min.js  dust-full-1.2.5.js      dust-full-2.2.3.js
dust-core-1.2.5.js      dust-core-2.2.3.js      dust-full-1.2.6.js      dust-full-2.2.3.min.js
dust-core-1.2.6.js      dust-core-2.2.3.min.js  dust-full-2.0.0.js
dust-core-2.0.0.js      dust-full-1.1.1.js      dust-full-2.0.0.min.js

这个“工作”项目的演示给了我同样的错误: https://github.com/chovy/express-template-demo

1 个答案:

答案 0 :(得分:5)

<强> [编辑]

这里讨论一个问题:

https://github.com/linkedin/dustjs/commit/e5ebff0f7b32f8ff0883be7f7924507b314eef1d

[/node_modules/]consolidate/lib/consolidate.js转到exports.dust.render功能:

...
try {
  var tmpl = cache(options) || cache(options, engine.compileFn(str));
  tmpl(options, fn);
} catch (err) {
  fn(err);
}
...

engine.compileFn被称为发送str作为参数,str实际上是模板本身:

Title: {title}
{p> engine.compileFn compiler.compile(source, name)中的[/node_modules/]dustjs-linkedin/lib/compiler.js

source是模板发送的,但name是模板名称,在我们的情况下应该是index,永远不会设置(未定义)。< / p>

或者,我更喜欢将Payapl的Adaro库用于express 3.xlinkedin-dustjs。它是他们的项目kraken.js的一部分,但可以作为独立的防尘包装用于表达:

https://github.com/paypal/adaro

如README.md中所述:

var express = require('express');
var dustjs = require('adaro');

var app = express();

app.engine('dust', dustjs.dust({});
app.set('view engine', 'dust');

// For rendering precompiled templates:
// app.engine('js', dustjs.js({ ... ));
// app.set('view engine', 'js');