heroku 错误:在视图目录“/app/views”中查找视图“index”失败

zysjyyx4  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(167)

每次我尝试将应用程序部署到heroku时都出现此错误。在本地主机上运行时工作完全正常:3000

2022-11-13T14:46:11.815962+00:00 heroku[web.1]: Starting process with command `node server.js`
2022-11-13T14:46:15.619107+00:00 app[web.1]: Server has started
2022-11-13T14:46:15.861854+00:00 app[web.1]: DB connected successfully
2022-11-13T14:46:16.018081+00:00 heroku[web.1]: State changed from starting to up
2022-11-13T14:52:47.472985+00:00 heroku[router]: at=info method=GET path="/" host=stark-lowlands-39210.herokuapp.com request_id=ef33e137-00ea-48d8-be4f-05b0c4fa505a fwd="12.205.212.138" dyno=web.1 connect=0ms service=27ms status=500 bytes=404 protocol=https
2022-11-13T14:52:47.477306+00:00 app[web.1]: Error: Failed to lookup view "index" in views directory "/app/views"
2022-11-13T14:52:47.477309+00:00 app[web.1]: at Function.render (/app/node_modules/express/lib/application.js:597:17)
2022-11-13T14:52:47.477309+00:00 app[web.1]: at ServerResponse.render (/app/node_modules/express/lib/response.js:1039:7)
2022-11-13T14:52:47.477311+00:00 app[web.1]: at /app/app.js:20:9
2022-11-13T14:52:47.477312+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2022-11-13T14:52:47.477312+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:144:13)
2022-11-13T14:52:47.477313+00:00 app[web.1]: at Route.dispatch (/app/node_modules/express/lib/router/route.js:114:3)
2022-11-13T14:52:47.477313+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2022-11-13T14:52:47.477314+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:284:15
2022-11-13T14:52:47.477315+00:00 app[web.1]: at Function.process_params (/app/node_modules/express/lib/router/index.js:346:12)
2022-11-13T14:52:47.477315+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/index.js:280:10)

我在下面包括了我的app.js、server.js和文件夹层次结构:

app.js

const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const mongoose  = require("mongoose");
const https = require("https");
// const File = require("./model/fileSchema");
const multer = require("multer");
const http = require('http');

const app = express();

// Set up code
app.set("view engine", "ejs");
app.set("views", path.join(__dirname + "/views"));
app.use(express.static(`${__dirname}/public`));
app.use(bodyParser.urlencoded({extended : true}));
app.use(express.static("public"));

app.get('/', function (req, res) {
    res.render("index");
});

server.js

const app = require("./app");
const mongoose = require("mongoose");
const http = require('http');
const https = require("https");
const path = require("path");

process.on("uncaughtException", (err) => {
  console.log("UNCAUGHT EXCEPTION, APP SHUTTING NOW!!");
  console.log(err.message, err.name);
  process.exit(1);
});

const DB = "mongodb+srv://boepartners:missyangus123@cluster0.dm8gvgf.mongodb.net/BOE";

mongoose
  .connect(DB, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    autoIndex: true,
  })
  .then(() => {
    console.log("DB connected successfully");
  });

  let port = process.env.PORT;
  if (port == null || port == "") {
    port = 3000;
  }

  app.listen(port, function(){
    console.log("Server has started");
  });

我希望index.ejs文件在部署到heroku时能够正确呈现。

kgqe7b3p

kgqe7b3p1#

若要呈现模板文件,请在app.js中设置以下应用程序设置属性。

  • views,模板文件所在的目录。例如:app.set('views','./views')。这默认为应用程序根目录中的views目录。
  • 视图引擎,要使用的模板引擎。例如,要使用ejs模板引擎:app.set('检视引擎','ejs')。
app.set('view engine', 'ejs');
app.set('views', 'views');

根据您的目录,app.jsviews在同一级别。因此,将属性更改为app.set('views', 'views')。这样就可以了。同时更改以下属性

app.use(express.static(path.join(__dirname, 'public')));

相关问题