我使用Jade模板引擎替换以替换我的变量:;我尝试过以下方法:
var express = require('express');
var jade = require('jade');
var router = express.Router();
router.get('/test', function (req, res, next) {
var html= res.render(jade.renderFile('test', {"message": "test1", "title": "test2"}));
res.render(html);
});
module.exports = router;
以下是我的test.jade:
html
head
title= title
body
h1= message
但我保留了以下错误:
消息:> "Failed to lookup view
> "<html><head><title>test2</title></head><body><h1>test1</h1></body></html>"
> in views directory "C:\node\myProj\views"",
Alos这是我在app.js中设置的jade:
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.engine('jade', require('jade').__express);
那么我做错了什么?任何帮助表示赞赏...
更新以使用res.render:
更新为使用app.render:
所以我改变了路线:
var express = require('express');
var router = express.Router();
var app;
module.exports = function (_app) {
app = _app;
return router;
}
router.get('/test', function (req, res, next) {
app.render('test', {"message": "test1", "title": "test2"}, function(err, html){
res.render(html);
});
});
现在我在app.js中以这种方式打电话:
var test = require('./routes/test');
app.use(test(app));
现在它给了我:
> "Failed to lookup view
> "<html><head><title>test2</title></head><body><h1>test1</h1></body></html>"
> in views directory "C:\node\myProj\views""
尽管res.render工作正常:
res.render('test', {"message": "test1", "title": "test2"});
答案 0 :(得分:1)
看起来你很近但你的请求处理程序需要稍微调整:
router.get('/test', function (req, res, next) {
res.render('test', {"message": "test1", "title": "test2"}));
});
res.render
现在足够智能为您做玉了./views
前缀和.jade
后缀,因为Express现在将这些视为默认值jade