我正在使用Express应用程序,但始终出现以下错误: 错误:无法在视图目录中查找视图“索引”。 我通过npm安装路径,并在server.js中需要它,但我仍然面临相同的错误。 如何解决此问题?感谢您的输入。
server.js:
var express = require("express");
var path = require('path');
var bodyParser = require("body-parser");
var methodOverride = require('method-override');
var PORT = process.env.PORT || 8081;
var app = express();
// serve static content for the app and set up body-parser
app.use(express.static(__dirname+'/public'));
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use(methodOverride('_method'));
// set up Handlebars
var exphbs = require("express-handlebars");
app.engine("handlebars", exphbs({
defaultLayout: "main"
}));
// app.set('views', path.join(__dirname, 'views'));
app.set("view engine", "handlebars");
// Import routes and give the server access to them
var routes = require("./controllers/routes.js");
app.use('/',routes);
// start the server listening
app.listen(PORT, function() {
console.log("App now listening at localhost:" + PORT);
});
routes.js:
var express = require('express');
var router = express.Router();
// Import the model to use its database functions.
var burger = require('../models/burger.js');
router.get('/', function(req, res) {
burger.all(function(burger_data){
console.log(burger_data);
res.render('index', {burger_data});
});
});
router.post('/burgers/create',function(req,res){
burger.create(req.body.burger_name, function(result){
res.redirect('/');
});
});
router.put('/burgers/update',function(req,res){
burger.update(req.body.burger_id, function(result){
console.log(result);
res.redirect('/');
});
});
module.exports = router;