每次我尝试将应用程序部署到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时能够正确呈现。
1条答案
按热度按时间kgqe7b3p1#
若要呈现模板文件,请在app.js中设置以下应用程序设置属性。
根据您的目录,
app.js
和views
在同一级别。因此,将属性更改为app.set('views', 'views')
。这样就可以了。同时更改以下属性