【问题标题】:node.js do not work after deploy on Heroku在 Heroku 上部署后 node.js 不起作用
【发布时间】:2021-09-04 04:38:57
【问题描述】:

我是 node.js 和 Heroku 的新手,我刚刚首次将 node.js 应用程序部署到 Heroku,当我在 Heroku 中运行时,应用程序没有运行,所以我使用了命令 heroku logs --tail 但我有这个错误: enter image description here

出于想法,我的 node.js 在没有 Heroku 的情况下工作得很好,但在那里部署后停止工作

这里是我的 index.js:

const express = require("express");
var md5 = require('md5');
var reverseMd5 = require('reverse-md5');
const app = express();
const userRouter = require("./API/users/user.router");
const operationsRouter = require("./API/operations/operations.router");


app.use(express.json());

app.use("", userRouter);
app.use("", operationsRouter);
app.use(express.static('images'));

const port = process.env.APP_PORT || 3000;
app.listen(port, () => {
    console.log("server up and running on PORT :", port);
});

英雄我的配置文件 java:

const { createPool } = require("mysql");
const pool = createPool({
    host: 'host.example.com',
    port: 3306,
    user: 'user',
    password: 'password',
    database: 'database',
    connectionLimit: 100,
    multipleStatements: true
});
module.exports = pool;

令我震惊的是,为什么这些错误只有在 Heroku 上部署后才会出现? 有什么办法可以解决这个问题吗?

【问题讨论】:

  • 确保删除您问题中的凭据,并使用您的数据库提供商重置它们。
  • 不是付费数据库,是免费的数据库主机,仅供测试

标签: node.js heroku


【解决方案1】:

Heroku 会为您的应用动态分配端口,因此您无法将端口设置为固定数字。 Heroku 将端口添加到环境中,因此您可以从那里拉出它。切换你的APP_PORT 听这个:

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

确保您已将APP_PORT 更改为PORT。 这样,当您在本地测试时,它仍然会监听端口 5000,但它也可以在 Heroku 上运行。

我可以看到您在 Heroku 上使用 Nodemon,这在 Heroku 上是不必要的。所以从你的 Heroku 中删除 Nodemon 并声明一个 Procfile

web: node app.js

成为app.js 应用程序的入口点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-22
    • 2017-04-09
    • 2021-04-30
    • 2017-01-12
    • 2021-06-12
    • 2015-12-22
    • 1970-01-01
    • 2019-09-05
    相关资源
    最近更新 更多