gpt4 book ai didi

node.js - 无法通过express.js和路径在views目录中查找 View

转载 作者:太空宇宙 更新时间:2023-11-03 22:10:31 25 4
gpt4 key购买 nike

我在通过node.js和express.js路由 View 时遇到问题我的项目树如下所示:

app/
app.js
views/
index.html
login.html
public/
css/
js/

我使用express和path;这是我设置路径的代码:

app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
//forms
app.set('views', path.join(__dirname, 'views'));
//public
app.use(express.static(path.join(__dirname, 'public')));

我收到此错误:

Error: Failed to lookup view "login.html" in views directory "/home/Project/V0.0.8_3/app/views"
at EventEmitter.render (/home/Project/V0.0.8_3/node_modules/express/lib/application.js:580:17)
at ServerResponse.render (/home/Project/V0.0.8_3/node_modules/express/lib/response.js:966:7)
at /home/Project/V0.0.8_3/app/routes/login.js:11:11
at Layer.handle [as handle_request] (/home/Project/V0.0.8_3/node_modules/express/lib/router/layer.js:95:5)
at next (/home/Project/V0.0.8_3/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/Project/V0.0.8_3/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/Project/V0.0.8_3/node_modules/express/lib/router/layer.js:95:5)
at /home/xvps/Tracker/V0.0.8_3/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/Project/V0.0.8_3/node_modules/express/lib/router/index.js:335:12)
at next (/home/Project/V0.0.8_3/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/home/Project/V0.0.8_3/node_modules/express/lib/router/index.js:174:3)
at router (/home/Project/V0.0.8_3/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/home/Project/V0.0.8_3/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/Project/V0.0.8_3/node_modules/express/lib/router/index.js:317:13)
at /home/Project/V0.0.8_3/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/Project/V0.0.8_3/node_modules/express/lib/router/index.js:335:12)

当我尝试从公共(public)目录获取文件时,出现此错误:

Cannot GET /js/login.js

实际上我知道本地路径和服务器路径之间的区别,并且我已经阅读了堆栈溢出中提出的很多相关问题,但我找不到我的错误是什么。

更新

路由代码在这里:

var router = express.Router();require('./router')(app);

router.js:

var routes = require('./routes/index');
var login = require('./routes/login');

module.exports = function (app) {

/* Index(main) route */
app.use('/', routes);
app.use('/index', routes);
app.use('/index.html', routes);
app.use('/login', login);
app.use('/login.html', login);
app.use('/logout', logout);
};

routes目录下有两个js文件; index.js 和 login.js

index.js:

var express = require('express');
var router = express.Router();

router.route('/')
// fetch all users
.get(function (req, res) {
console.log('get method');
if(req.session.logged) {
res.render('index.html');
}
else {
res.redirect('/login');
}

});
module.exports = router;

最佳答案

您使用的是哪个 Express 版本?

已更新

在express 4中,中间件是内置在外部模块中的,因此对于您的情况下的静态文件,您应该使用serve-static中间件:

示例代码:

var express = require('express') 
var path = require('path')
var serveStatic = require('serve-static')
var app = express()
app.use(serveStatic(path.join(__dirname, 'public')))
app.listen(3000)

关于node.js - 无法通过express.js和路径在views目录中查找 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43529740/

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