Error: Failed to lookup view "/packages" in views directory

Error: Failed to lookup view "/packages" in views directory

我已经在我的视图文件夹中设置了 handelbars 视图 "packages"。 我的 "routes" 文件夹中有一个 users.js 文件,设置如下:

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

//Get about
router.get('/about', function(req,res){
    res.render('about');
});

//Get services
router.get('/services', function(req,res){
    res.render('services');
});

//Get packages
router.get('/packages', function(req,res){
    res.render('/packages');
});

//Get incentives
router.get('/incentives', function(req,res){
    res.render('/incentives');
});

module.exports = router;

我的 index.js 文件也在 "routes" 文件夹中:

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

//Get Homepage
router.get('/', function(req,res){
    res.render('index');
});

module.exports = router;

我的app.js文件如下:

var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var exphbs = require('express-handlebars');

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

//Init App
var app = express();

//View Engine
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', exphbs({defaultLayout: 'layout'}));
app.set('view engine', 'handlebars');

//Set Static Folder
app.use(express.static(path.join(__dirname, 'public')));

//BodyParser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

app.use('/', routes);
app.use('/users', users);

//Set Port
app.set('port', (process.env.PORT || 3000));

app.listen(app.get('port'), function(){
    console.log('Server started on port '+ app.get('port'))
}); 

我不明白在激励和套餐视图方面我做错了什么,因为其他视图工作正常。有人可以帮忙吗?

您需要从奖励和套餐路线的 res.render... 语句中删除前导 /

希望对您有所帮助!

正如上面大卫指出的那样,确保你的路线没有前导 /

//Get about
router.get('/about', (req,res) => {
    res.render('about');
});

//Get services
router.get('/services', (req,res) => {
    res.render('services');
});

//Get packages
router.get('/packages', (req,res) => {
    res.render('packages');
});

//Get incentives
router.get('/incentives', (req,res) => {
    res.render('incentives');
});