TypeError:视图不是构造函数EJS

时间:2018-08-14 11:56:37

标签: node.js express render typeerror ejs

当我尝试呈现EJS页面时出现此错误...我猜该错误就在我这一边,所以请您帮我调试一下吧?

错误

TypeError: View is not a constructor
at Function.render (/home/bot_king/nodelearn/project/node_modules/express/lib/application.js:570:12)
at ServerResponse.render (/home/bot_king/nodelearn/project/node_modules/express/lib/response.js:1008:7)
at /home/bot_king/nodelearn/project/app.js:16:9
at Layer.handle [as handle_request] (/home/bot_king/nodelearn/project/node_modules/express/lib/router/layer.js:95:5)
at next (/home/bot_king/nodelearn/project/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/bot_king/nodelearn/project/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/bot_king/nodelearn/project/node_modules/express/lib/router/layer.js:95:5)
at /home/bot_king/nodelearn/project/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/bot_king/nodelearn/project/node_modules/express/lib/router/index.js:335:12)
at next (/home/bot_king/nodelearn/project/node_modules/express/lib/router/index.js:275:10)

代码

var express = require("express");
var app = express();
var port = 8080;
var ejs = require('ejs');
app.use(express.static("src/view"));

app.set("view", "./src/view");
app.set("view engine", "ejs");

app.use(express.static("public"));
app.listen(port, function(err){
console.log("The server is running on port " + port);
});

app.get("/", function(req, res){
res.render("index", { list: ["first", "2nd", "3rd"]})
});

HTML

<ul>
<% for(var i = 0; i<list.length; i++){%>
    <li> <h4> <%=list[i] %> </h4> </li>
<%}%>
</ul>

1 个答案:

答案 0 :(得分:2)

应该是视图,而不是视图,因此您必须更改以下行,

app.set("view", "./src/view");

app.set("views", "./src/view");

如果您仍然遇到问题(即使进行了更改,也请尝试从上述语句中删除./src片段,并将其设置为

app.set("view", "./view");

希望这会有所帮助!