我似乎没有让JSHTML作为Node.js中Express.js的模板引擎。当我安装我的Express.js应用程序并为我创建一个基本应用程序时,我运行它会收到以下错误消息:
500 TypeError: Property 'engine' of object #<View> is not a function
at View.render (/Users/blackbook/nodejs/ds/node_modules/express/lib/view.js:75:8)
at Function.app.render (/Users/blackbook/nodejs/ds/node_modules/express/lib/application.js:504:10)
at ServerResponse.res.render (/Users/blackbook/nodejs/ds/node_modules/express/lib/response.js:677:7)
at exports.index (/Users/blackbook/nodejs/ds/routes/index.js:7:7)
at callbacks (/Users/blackbook/nodejs/ds/node_modules/express/lib/router/index.js:165:11)
at param (/Users/blackbook/nodejs/ds/node_modules/express/lib/router/index.js:139:11)
at pass (/Users/blackbook/nodejs/ds/node_modules/express/lib/router/index.js:146:5)
at Router._dispatch (/Users/blackbook/nodejs/ds/node_modules/express/lib/router/index.js:173:5)
at Object.router (/Users/blackbook/nodejs/ds/node_modules/express/lib/router/index.js:33:10)
at next (/Users/blackbook/nodejs/ds/node_modules/express/node_modules/connect/lib/proto.js:190:15)
我的app.js看起来像这样(这是Express.js为我创建的):
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jshtml');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
我有这个装置:
答案 0 :(得分:3)
JSHTML目前适用于Express.js 2.有计划让引擎与Express.js 3一起使用,但目前我忙于享受夏天!期待在冬天解决这个问题!
答案 1 :(得分:2)
根据https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x,您可以使用app.engine实现2-x兼容性。
e.g。
var fs = require("fs");
var jshtml = require("jshtml");
app.engine("jshtml", function (path, options, fn) {
fs.readFile(path, 'utf8', function (err, str) {
if (err) return fn(err);
str = jshtml.compile(str,options)(options).toString();
fn(null, str);
});
});
答案 2 :(得分:0)
consolidate.js用作许多模板引擎和表达之间的桥梁。如果您的引擎不受支持,请检查源代码。大多数引擎需要15行代码才能实现。
答案 3 :(得分:0)
我让它在我的项目中工作,可能会很快发出拉取请求,但现在看看我的评论 https://github.com/elmerbulthuis/jshtml/issues/5
答案 4 :(得分:0)
尝试以下方法。它和我一样适合我。
首先,通过npm安装jshtml-express,然后执行以下操作。
var app = express();
**app.engine('jshtml', require('jshtml-express'));**
// All environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jshtml');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
我希望它也适合你。