PERN 堆栈 - 成功部署静态网页,但在 Heroku 部署中出现 500 内部服务器错误

时间:2021-02-17 05:44:22

标签: node.js express heroku deployment

我已经在 Heroku 上成功部署了一个 PERN 堆栈。该应用程序在本地完美运行,但在部署时无法连接。它运行静态页面,但没有连接到 API。

错误是我收到 500 内部服务器错误。我为客户端连接 localhost:3000,为服务器连接 localhost:3001。我正在使用代理来替换我的路由的 baseURL。我已经为 Heroku 插件设置了 DATABASE_URL。同样在部署过程中,我在部署前在 Heroku 中创建了必要的表。

我曾多次尝试部署并寻找解决方案,但都没有成功。任何帮助将不胜感激。

错误:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)
main.28345b5c.chunk.js:1 Error: Request failed with status code 500
    at e.exports (2.09b0e732.chunk.js:2)
    at e.exports (2.09b0e732.chunk.js:2)
    at XMLHttpRequest.p.onreadystatechange (2.09b0e732.chunk.js:2)

DB.js:

const devConfig = `postgresql://${process.env.PG_USER}:${process.env.PG_PASSWORD}@${process.env.PG_HOST}:${process.env.PG_PORT}/${process.env.PG_DATABASE}`;

const proConfig = process.env.DATABASE_URL; //heroku addons

const pool = new Pool({
  connectionString:
    process.env.NODE_ENV === 'production' ? proConfig : devConfig,
});

module.exports = pool;

Server.js:

require('dotenv').config();
const cors = require('cors');
const db = require('./db');
const express = require('express');
const app = express();
const path = require('path');
const morgan = require('morgan');
const { validateData } = require('./utils/validators');
const port = process.env.PORT || 3001;
app.use(express.json());
app.use(morgan('dev'));
app.use(cors());

if (process.env.NODE_ENV === 'production') {
//server static content
//npm run build
app.use(express.static(path.join(__dirname, 'client/build')));
}

app.get('/api/v1/restaurants', async (req, res) => {
=============== Routes ==============================

Package.json:

  "engines": {
    "node": "12.17.0",
    "npm": "7.5.2"
  },
  "scripts": {
    "start": "node server.js",
    "heroku-postbuild": "cd client && npm install && npm run build"
  }

Heroku 日志

2021-02-17T05:22:57.000000+00:00 app[api]: Build succeeded
2021-02-17T05:23:05.416267+00:00 heroku[web.1]: Starting process with command `node server.js`
2021-02-17T05:23:07.507510+00:00 app[web.1]: Server is running on 11615
2021-02-17T05:23:07.843998+00:00 heroku[web.1]: State changed from starting to up
2021-02-17T05:23:52.998817+00:00 app[web.1]: GET / 200 10.670 ms - 2469
2021-02-17T05:23:53.148888+00:00 app[web.1]: GET /static/js/main.28345b5c.chunk.js 200 1.092 ms - 15240
2021-02-17T05:23:53.152708+00:00 app[web.1]: GET /static/js/2.09b0e732.chunk.js 200 1.234 ms - 184584
2021-02-17T05:23:52.997093+00:00 heroku[router]: at=info method=GET path="/" host=restaurant-finder-1.herokuapp.com request_id=2f69d145-9c3b-4758-a9c3-b44e98b25d7e fwd="154.20.117.96" dyno=web.1 connect=0ms service=17ms status=200 bytes=2790 protocol=https
2021-02-17T05:23:53.153700+00:00 heroku[router]: at=info method=GET path="/static/js/main.28345b5c.chunk.js" host=restaurant-finder-1.herokuapp.com request_id=f25e4d0e-dd3f-45f2-84c5-234aab40e933 fwd="154.20.117.96" dyno=web.1 connect=0ms service=7ms status=200 bytes=15576 protocol=https
2021-02-17T05:23:53.754192+00:00 heroku[router]: at=info method=GET path="/api/v1/restaurants" host=restaurant-finder-1.herokuapp.com request_id=13f4dbdb-b41d-4a17-be22-9957cfe35dbc fwd="154.20.117.96" dyno=web.1 connect=1ms service=21ms status=500 bytes=396 protocol=https
2021-02-17T05:23:53.752767+00:00 app[web.1]: GET /api/v1/restaurants 500 18.514 ms - 137
2021-02-17T05:23:54.028676+00:00 heroku[router]: at=info method=GET path="/manifest.json" host=restaurant-finder-1.herokuapp.com request_id=679b77b1-cad8-43a8-a9be-e72e3af326c2 fwd="154.20.117.96" dyno=web.1 connect=0ms service=3ms status=200 bytes=819 protocol=https

0 个答案:

没有答案
相关问题