我正在通过快递使用带有js渲染的车把我遇到了解决错误的问题。每次我运行localserver / 300时,都会出现错误:无法在views目录中查找view“ error”。不知道我是否缺少什么,请在下面查看我的代码。
the error
Error: Failed to lookup view "error" in views directory "C:\Users\Sue\myCommunityAppCh3\views"
// view engine setup
var createError = require('http-errors');
var express = require('express');
var exphbs = require('express-handlebars');
var path = require('path');``
var cookieParser = require('cookie-parser');
var logger = require('morgan');`
var indexRouter = require('./app_server/routes/index');
var usersRouter = require('./app_server/routes/users');
var app = express();
// view engine setup
app.set('view engine','handlebars');
app.set('views', path.join(__dirname, '/public/views'));
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.use(express.static(path.join(__dirname, 'public')));
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
enter code hereenter code here
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
答案 0 :(得分:0)
我看不到太多代码,但是请确保您的views文件夹位于public内,并确保您告诉express将public用作静态路径(app.use(express.static('public'))
)。