NodeJS TypeError:视图不是构造函数EJS

np8igboo  于 2023-02-08  发布在  Node.js
关注(0)|答案(1)|浏览(137)
    • 嗨**

当我试图呈现一个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"]})
});
    • 超文本标记语言**
<ul>
<% for(var i = 0; i<list.length; i++){%>
    <li> <h4> <%=list[i] %> </h4> </li>
<%}%>
</ul>
sz81bmfz

sz81bmfz1#

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

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

如,

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

如果您仍然面临这个问题(即使在进行更改之后,也可以尝试从上面的语句中删除./src片段,并将其设置为,

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

希望这有帮助!
参见文档中的express.js设置表。有views属性。

相关问题