EXPRESS:错误:无法在视图目录中查找视图&Quot;index"

2022-03-23 00:00:00 express javascript ejs

我将NodeJS模板引擎设置为ejs。当我使用ejs模板运行app.js时,收到错误";Failed to lookup view‘error’in views";

Error: Failed to lookup view "index" in views directory "/home/runner/kaskusttv/views"
    at Function.render (/home/runner/kaskusttv/node_modules/express/lib/application.js:580:17)
    at ServerResponse.render (/home/runner/kaskusttv/node_modules/express/lib/response.js:1012:7)
    at /home/runner/kaskusttv/app.js:72:6
    at Layer.handle [as handle_request] (/home/runner/kaskusttv/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/runner/kaskusttv/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/runner/kaskusttv/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/runner/kaskusttv/node_modules/express/lib/router/layer.js:95:5)
    at /home/runner/kaskusttv/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/runner/kaskusttv/node_modules/express/lib/router/index.js:335:12)
    at next (/home/runner/kaskusttv/node_modules/express/lib/router/index.js:275:10)
    at compression (/home/runner/kaskusttv/node_modules/compression/index.js:220:5)
    at Layer.handle [as handle_request] (/home/runner/kaskusttv/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/runner/kaskusttv/node_modules/express/lib/router/index.js:317:13)
    at /home/runner/kaskusttv/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/runner/kaskusttv/node_modules/express/lib/router/index.js:335:12)
    at next (/home/runner/kaskusttv/node_modules/express/lib/router/index.js:275:10)

让这些在快递中工作的合适位置是什么?我使用此选项将引擎设置为ejs

app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");

解决方案

app.set("views", path.join(__dirname, "views"));

此行表示:Express应用程序将使用";views";文件夹中的ejs文件呈现HTML

Error: Failed to lookup view "index" in views directory "/home/runner/kaskusttv/views"

此错误表示:Express应用程序尝试在"/home/runner/kaskusttv/views"中搜索index.ejs文件,但无法搜索。

您的设置是正确的,您只需将index.ejs文件放入";views";文件夹(并且";views";文件夹位于您的应用的根目录中)。

我创建了此沙箱,请随意测试:https://glitch.com/edit/#!/express-ejs-sandbox0

相关文章