dustjs-linkedin with express 3

人盡茶涼 提交于 2019-12-08 13:37:06

问题


I am trying to get dustjs-linkedin working with an express 3 project, however I can't seem to get past this error:

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)

I can reproduce this with a brand new express 3 project like so:

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": "*"
  }
}

routes/index.js

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

views/index.dust

Title: {title}

The strange thing is, I have a project using express 3 and dustjs-linkedin that is working just fine. I ran a diff on the files in the dustjs-linkedin module and the working version has a lot of files in dist while the project throwing the error only has a few, namely:

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

This demo of a 'working' project gives me the same error: https://github.com/chovy/express-template-demo


回答1:


[Edit]

An issue is discussed here:

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

In [/node_modules/]consolidate/lib/consolidate.js go to exports.dust.render function:

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

engine.compileFn is called sending str as parameter, str is actually the template itself:

Title: {title}

engine.compileFn is compiler.compile(source, name) in [/node_modules/]dustjs-linkedin/lib/compiler.js.

source is template sent, however name which is the template name and should be index in our case, is never set (undefined).

Alternatively, I prefer to use Payapl's Adaro library for express 3.x and linkedin-dustjs. It's part of their project kraken.js, but can be used as a standalone dust wrapper for express:

https://github.com/paypal/adaro

As mentioned in 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');


来源:https://stackoverflow.com/questions/21217070/dustjs-linkedin-with-express-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!