gpt4 book ai didi

javascript - Node.js:无法查找 View

转载 作者:太空宇宙 更新时间:2023-11-04 02:26:34 24 4
gpt4 key购买 nike

这是当我运行“Node server.js”并获得“监听”作为响应,然后打开浏览器找到它时得到的结果:

Error: Failed to lookup view "./login/login" in views directory "C:\Users\Ghanem\cloud1\views"
at EventEmitter.app.render (C:\Users\Ghanem\cloud1\node_modules\express\lib\application.js:555:17)
at ServerResponse.res.render (C:\Users\Ghanem\cloud1\node_modules\express\lib\response.js:938:7)
at C:\Users\Ghanem\cloud1\server.js:226:7
at Layer.handle [as handle_request] (C:\Users\Ghanem\cloud1\node_modules\express\lib\router\layer.js:82:5)
at next (C:\Users\Ghanem\cloud1\node_modules\express\lib\router\route.js:110:13)
at Route.dispatch (C:\Users\Ghanem\cloud1\node_modules\express\lib\router\route.js:91:3)
at Layer.handle [as handle_request] (C:\Users\Ghanem\cloud1\node_modules\express\lib\router\layer.js:82:5)
at C:\Users\Ghanem\cloud1\node_modules\express\lib\router\index.js:267:22
at Function.proto.process_params (C:\Users\Ghanem\cloud1\node_modules\express\lib\router\index.js:321:12)
at next (C:\Users\Ghanem\cloud1\node_modules\express\lib\router\index.js:261:10)

我将“views”文件夹从“C:\Users\Ghanem\cloud1\app\server\views”移动到“C:\Users\Ghanem\cloud1\views”,但仍然出现相同的错误(我现在将其移回来)。

这是来自 server.js 的“./login/login”代码行:

app.get("/login", function (req, res) {
// Show form, default value = current username
res.render("./login/login", { "username": req.session.username, "error": null });
});

结构如下:

./
app/
public/
css/
style.css
style.styl

img/
glyph.png
glyph1.png

js/
controllers/
homeController.js
loginController.js
signupController.js

form-validators/
accountValidator.js
emailValidator.js
loginValidator.js
resetValidator.js
views/
home.js
login.js
reset.js
signup.js


vendor/
bootstrap.min.css
bootstrap-modal.js
bootstrap-transition.js
jquery.form.js
jquery.min.js


server/
modules/
account-manager.js
country-list.js
email-dispatcher.js
email-settings.js
views/
el/
Models/
alert.jade
confirm.jade
form-errors.jade
lost-password.jade
reset-password.jade

404.jade
account.jade
home.jade
layout.jade
login.jade
print.jade
reset.jade
signup.jade


router.js
router.js.b


router.js
router.js.b

如何解决这个问题?请记住,我已经将 View 文件夹移至根文件夹,但不知何故未检测到它。

最佳答案

根据您的评论:

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

所以如果你写:

app.get("/login", function (req, res) {
// Show form, default value = current username
res.render("login/login", { "username": req.session.username, "error": null });
});

如果您的 index.js 位于 app/server 文件夹中,则模板必须位于 app/server/views/login/login.ejs 中。

关于javascript - Node.js:无法查找 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30059047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com