我有问题通过node.js和express.js路由视图 我的项目树看起来像:
app/
app.js
views/
index.html
login.html
public/
css/
js/
我使用快递和路径;这是我设置路径的代码:
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)
当我尝试从公共目录中获取文件时,我收到此错误:
Cannot GET /js/login.js
实际上我知道本地路径和服务器路径有所不同,我已经阅读了堆栈溢出中已经提到的很多相关问题,但我找不到我的错误。
更新
路由代码在这里:
var router = express.Router(); 要求( './路由器')(应用程序);
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;
答案 0 :(得分:1)
您使用的是哪种快递版本?
在Express 4中间件是在外部模块中构建的,所以对于你的静态文件,你应该使用serve-static中间件:
示例代码:
public class MovieSession implements Comparable<MovieSession> {
private String movieName;
private char rating;
private Time sessionTime;
private SeatReservation[][] sessionSeats;
public static int NUM_ROWS;
public static int NUM_COLS;
public MovieSession(String movieName, char rating, Time sessionTime) {
this.movieName = movieName;
this.rating = rating;
this.sessionTime = sessionTime;
}
This is the method where it gives me the error:
@Override
public int compareTo(MovieSession currentMovieSession) {
if (this.sessionTime < currentMovieSession.sessionTime)
{
return -1;
}
else if (this.sessionTime > currentMovieSession.sessionTime)
{
return 1;
}
if(this.sessionTime == currentMovieSession.sessionTime)
{
return this.movieName > currentMovieSession.movieName ? 1 : -1;
}
}
答案 1 :(得分:0)
因为我是使用node.js进行编码并上传到服务器上的初学者,所以我正在通过 npm
在服务器上运行我的代码,通过帮助我的朋友,我发现我必须运行 pm2
来获取实时应用。
答案 2 :(得分:0)
我也遇到了这个错误,但在精神上有些疲劳后,我能够解决这个问题。这是对我来说很好用的解决方案。
将这行代码 app.set('views', path.join(__dirname, 'views'));
更改为 app.set('views', path.join(__dirname, '../views'));
this。