ejs在渲染模板时抱怨未定义的变量

时间:2015-08-10 01:31:42

标签: node.js

下面显示的代码通过ejs运行下面的模板文件。意图是<%= query.animal%>被评估。相反,我收到了“查询未定义”的投诉。任何人都可以帮我解释为什么会这样吗?谢谢。

... doug

var http = require('http');
var fs = require("fs");
var ejs = require("ejs");
var server = http.createServer(function (request, response) {
  var query = require('url').parse(request.url,true).query;
    response.writeHead(200, {"Content-Type": "text/html"});
    response.end(ejs.render(fs.readFileSync('infile.html','utf8')));
});
server.listen(8000,'208.43.220.79');
console.log("Server running at http://208.43.220.79:8000/");

这是文件:

!DOCTYPE html>
<html>
 <head><title>Test 2</title></head>
 <body>
  <h2>My <%= query.animal %> has fleas.</h2>
 </body>
</html>

1 个答案:

答案 0 :(得分:0)

您必须明确地将变量传递给模板:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>3.0.5.RELEASE</version>
</dependency>