gpt4 book ai didi

node.js - dustjs-linkedin 与 Express 3

转载 作者:太空宇宙 更新时间:2023-11-04 02:58:48 26 4
gpt4 key购买 nike

我正在尝试让 dustjs-linkedin 与 Express 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)

我可以用一个全新的express 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": "*"
}
}

routes/index.js

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

views/index.dust

Title: {title}

奇怪的是,我有一个使用express 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

最佳答案

[编辑]

这里讨论一个问题:

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}

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

source 是发送的模板,但是 name 是模板名称,在我们的例子中应该是 index,但从未设置(未定义)。

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

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');

关于node.js - dustjs-linkedin 与 Express 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21217070/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com