尝试在 path= /favicon.ico 和 path=/ 上运行我的 Node.js 应用程序时,在 Heroku 出现错误

问题描述

//2a6595e9-b280-4ff2-b95e-64e67203ae36

const uri = "mongodb+srv://<email>:<password>@cluster0.eky9x.mongodb.net/<dbname>?retryWrites=true&w=majority"

const express = require("express");
const app = express();
const path = express
const bodyParser = require("body-parser");
const config = require("./modules/config");
const mongoose = require("mongoose");
const expressValidator = require("express-validator");
global.config = require("./modules/config");

//Connect to DB
mongoose.connect(uri,{ useNewUrlParser: true,useUnifiedTopology: true,useCreateIndex: true },(err,db) => console.log("connected to DB!"));
mongoose.Promise = global.Promise;
//

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json({ type: "application/json" }));
app.use(expressValidator());

const apiRouter = require("./modules/routes/api");
const panelRouter = require("./modules/models/controllers/v1/adminPanel");

app.use("/portfolio/api",apiRouter);
app.use("/portfolio/admin",panelRouter);

app.get('/',(req,res) => {
    res.sendFile(__dirname + '/index.html');
});
app.get('/favicon.ico',res) => res.status(204));

app.listen(process.env.PORT || 5000)

这是我的 server.js

我花了将近 5 个小时才弄明白。

所以起初我在 cPanel 上安装了我的应用程序,但它崩溃了。比我在 Heroku 上试过之后,在我记录了错误之后,我在那里看到了这些

2020-12-27T15:47:46.889808+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=nodejs-portfolio-server.herokuapp.com request_id=1e1793b9-e811-4234-bd9b-c20f3b74e987 fwd="95.223.73.133" dyno= connect= service= status=503 bytes= protocol=https
2020-12-27T15:47:47.182099+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=nodejs-portfolio-server.herokuapp.com request_id=4399a1ad-d48c-403f-91ae-1366c1b87c5d fwd="95.223.73.133" dyno= connect= service= status=503 bytes= protocol=https 

它在我的本地主机上运行得非常好,但在 Heroku 或 cPanel 上却没有。

到目前为止我已经尝试过这些:

它正在听 3000 但我已经用 app.listen(process.env.PORT || 5000) 和其他类似的东西替换了几次,但它没有用。我也试过没有这条线。

我也尝试过捕获该路径并向其发送一些文件甚至一些状态代码,但它也不起作用。

类似:

app.get('/',res) => res.status(204));

现在我快要哭了。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)