Express - 无法查找视图

Express - cannot lookup view

我咨询过很多其他类似的话题。我一直没能找到答案。

./
  config/
    connection.js
  controllers/
    homeRoutes.js
    index.js
  db/
    schema.db
  models/
    index.js
    User.js
  public/
    css/
      styles.css
  views/
    layouts/
      main.handlebars
  server.js

这是 homeRoutes.js 的样子:

const router = require('express').Router();
const { User, BlogPost, Comment } = require('../models');


//TEST - render homepage
router.get('/test', async (req, res) => {
    try {
        res.render('main');
    } catch (err) {
        res.status(500).json(err);
    }
});

这是 server.js 的内容(我认为问题出在此处):

// DEPENDENCIES
const express = require('express');
const sequelize = require('./config/connection');
const routes = require('./controllers');
const exphbs = require('express-handlebars');
const path = require('path');


// HANDLEBARS configuration
// set up Handlebars.js engine
const hbs = exphbs.create({});

// EXPRESS CONFIGURATION
// tells node that we are creating an express server
const app = express();  

// sets an initial port.
const PORT = process.env.PORT || 9999;

// inform Express.js on which template engine to use
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');

// Sets up express app to handle data parsing
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
app.use(require('./controllers/homeRoutes'));


// tells app what routes to use
app.use(routes);

// Logging DB info
console.log(`Database name: ${sequelize.config.database} \n running on port: 
${sequelize.config.port} \n under hostname: ${sequelize.config.host}`)


// Listener. Ths effectively 'starts' our server
app.listen(PORT, () => {
    console.log(`Node Server running on port: ${PORT}`);
})

当我 运行 服务器并转到我的路线时,即 localhost:9999/test 我收到以下错误:

Error: Failed to lookup view "main" in views directory "/Users/ep9k/Desktop/Coding Bootcamp/Week14/ur-rich-fsf-pt-02-2021-u-c/Bootcamp14_TechBlog/views"
    at Function.render (/Users/ep9k/Desktop/Coding Bootcamp/Week14/ur-rich-fsf-pt-02-2021-u-c/Bootcamp14_TechBlog/node_modules/express/lib/application.js:580:17)
    at ServerResponse.render (/Users/ep9k/Desktop/Coding Bootcamp/Week14/ur-rich-fsf-pt-02-2021-u-c/Bootcamp14_TechBlog/node_modules/express/lib/response.js:1012:7)
    at /Users/ep9k/Desktop/Coding Bootcamp/Week14/ur-rich-fsf-pt-02-2021-u-c/Bootcamp14_TechBlog/controllers/homeRoutes.js:8:13
    at Layer.handle [as handle_request] (/Users/ep9k/Desktop/Coding Bootcamp/Week14/ur-rich-fsf-pt-02-2021-u-c/Bootcamp14_TechBlog/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/ep9k/Desktop/Coding Bootcamp/Week14/ur-rich-fsf-pt-02-2021-u-c/Bootcamp14_TechBlog/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/ep9k/Desktop/Coding Bootcamp/Week14/ur-rich-fsf-pt-02-2021-u-c/Bootcamp14_TechBlog/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/ep9k/Desktop/Coding Bootcamp/Week14/ur-rich-fsf-pt-02-2021-u-c/Bootcamp14_TechBlog/node_modules/express/lib/router/layer.js:95:5)
    at /Users/ep9k/Desktop/Coding Bootcamp/Week14/ur-rich-fsf-pt-02-2021-u-c/Bootcamp14_TechBlog/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/ep9k/Desktop/Coding Bootcamp/Week14/ur-rich-fsf-pt-02-2021-u-c/Bootcamp14_TechBlog/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/ep9k/Desktop/Coding Bootcamp/Week14/ur-rich-fsf-pt-02-2021-u-c/Bootcamp14_TechBlog/node_modules/express/lib/router/index.js:275:10)

如果我将 main.handlebars 上移到视图目录中的一级,我会收到以下错误:

Error: ENOENT: no such file or directory, open '/Users/ep9k/Desktop/Coding Bootcamp/Week14/ur-rich-fsf-pt-02-2021-u-c/Bootcamp14_TechBlog/views/layouts/main.handlebars'

如果有帮助,这里是 github 回购:https://github.com/epurpur/Bootcamp14_TechBlog

我想你应该看看这里reference。您忘记在 views 文件夹中添加文件。在 homeRoutes.js 中,您将其称为 main 我谦虚地认为这令人困惑..在参考文献中的示例中,您有 home)。因此,如果您创建此文件,那么此代码将毫无问题地运行!