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

Express: Error: Failed to lookup view quot;indexquot; in views directory(EXPRESS:错误:无法在视图目录中查找视图Quot;indexquot;)
本文介绍了EXPRESS:错误:无法在视图目录中查找视图&Quot;index"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将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

这篇关于EXPRESS:错误:无法在视图目录中查找视图&Quot;index"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)