【问题标题】:Internal server error when deployed to Heroku部署到 Heroku 时出现内部服务器错误
【发布时间】:2019-05-22 16:37:22
【问题描述】:

现在我的应用已部署到 heroku,我收到了一个内部服务器错误。一切都在当地运作良好。我已经在这里工作了好几天了,所以我决定与社区联系!!!!

我已尝试更改 express 和 react 中的路径,但仍然没有任何效果。

这是我的快递和路线文件

const express = require("express");
const mongo_uri = require("./config/db");

const path = require("path");
const bodyParser = require("body-parser");
const MongoClient = require("mongodb").MongoClient;
const helmet = require("helmet");
const cors = require("cors");
const backingRouter = require("./routes/backingRouter");
const chordsRouter = require("./routes/chordsRouter");
const arpeggiosRouter = require("./routes/arpeggiosRouter");

const app = express();

app.use(helmet());
app.use(cors());

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// Handle CORS
app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "*");
  if (req.method === "OPTIONS") {
    res.header("Access-Control-Allow-Methods", "PUT, POST, PATCH, DELETE, GET");
    return res.status(200).json({});
  }
  next();
});

app.use("/api/backing-tracks", backingRouter);
app.use("/api/chords", chordsRouter);
app.use("/api/arpeggios", arpeggiosRouter);

// Production mode
if (process.env.NODE_ENV === "production") {
  app.use(express.static("client/build"));
  app.get("*", (req, res) => {
    res.sendFile(path.resolve((__dirname, "client", "build", "index.html")));
  });
}

const PORT = process.env.PORT || 5000;

app.listen(PORT, () => console.log(`Server Started`));

MongoClient.connect(mongo_uri, { useNewUrlParser: true }).then(client => {
  const db = client.db("backingTrackData");
  const collection = db.collection("backings");
  app.locals.collection = collection;
  console.log("MongoDb running...");
});
const express = require("express");
const router = express.Router();

router.get("/", (req, res, next) => {
  const { collection } = req.app.locals;
  collection

    .find({ tonality: { $in: ["major", "minor"] } })
    .toArray()
    .then(response => res.status(200).json(response))
    .catch(error => res.status(500).json(error));
});

module.exports = router;

这是一个反应文件

import React, { Component } from "react";
import axios from "axios";

import SelectTonality from "./SelectTonality";
import SelectTempo from "./SelectTempo";
import Dropdown from "./Dropdown";

import ReactAudioPlayer from "react-audio-player";

class backingTracks extends Component {
  state = {
    backingTracksMaj: [],
    backingTracksMin: [],
    toggleMenu: false,
    selectedBtn: "major",
    selectedTempo: "90",
    selectedTrack: "",
    selectedTrackName: ""
  };

  componentDidMount() {
    axios
      .get("/api/backing-tracks")
      .then(res => {
        const backingTracksMaj = res.data
          .filter(track => track.tonality === "major")
          .map(newTrack => {
            return newTrack;
          });
        const backingTracksMin = res.data
          .filter(track => track.tonality === "minor")
          .map(newTrack => {
            return newTrack;
          });

        this.setState({ backingTracksMaj, backingTracksMin });
      })
      .catch(error => console.log(error));
  }

主页加载正常,但是当我导航到背景页面或任何其他页面时,我得到: GET https://jazz-guitar-woodshed.herokuapp.com/api/backing-tracks500(内部服务器错误)

提前感谢您的帮助!!!

编辑: Heroku 错误

2019-05-22T23:34:23.840908+00:00 app[web.1]: TypeError: Cannot read property 'find' of undefined 
2019-05-22T23:34:23.840921+00:00 app[web.1]: at router.get (/app/routes/backingRouter.js:8:6) 
2019-05-22T23:34:23.840924+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5) 
2019-05-22T23:34:23.840926+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:137:13) 
2019-05-22T23:34:23.840927+00:00 app[web.1]: at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
2019-05-22T23:34:23.840929+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5) 
2019-05-22T23:34:23.840931+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:281:22 
2019-05-22T23:34:23.840933+00:00 app[web.1]: at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12) 
2019-05-22T23:34:23.840938+00:00 app[web.1]: at router (/app/node_modules/express/lib/router/index.js:47:12) 
2019-05-23T00:08:42.152915+00:00 heroku[web.1]: Idling 
2019-05-23T00:08:42.157365+00:00 heroku[web.1]: State changed from up to down 
2019-05-23T00:08:43.157938+00:00 heroku[web.1]: Stopping all processes with SIGTERM 
2019-05-23T00:08:43.269315+00:00 heroku[web.1]: Process exited with status 143
2019-05-22T23:34:23.840935+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/index.js:275:10)

【问题讨论】:

  • 它在本地工作吗?
  • 我认为您可能必须将完整的 URL 地址放入 .get() 函数中。既然你在 heroku 上,那就是.get('https://jazz-guitar-woodshed.herokuapp.com/api/backing-tracks')
  • 它在本地运行没有问题。此外,我在 package.json 文件中设置了一个代理,它应该让您只需在 .get() 中输入路径(我认为这就是它的工作方式)。我可以在回家后尝试使用完整路径,看看是否可行。谢谢!
  • heroku 日志 --tail ???
  • 我会在我回到家时添加。谢谢

标签: node.js reactjs express heroku internal-server-error


【解决方案1】:

也许不是这个

app.use(bodyParser.urlencoded({ extended: false }));

试试这个:

app.use(express.json());
app.use(express.urlencoded({extended: true})); 

将扩展设置为 true 允许通过 express 发送嵌套的 JSON 对象,您的对象显然就是这样。 Vanilla express 也会做 bodyParser 做的事情。所以你真的不需要它。

【讨论】:

    【解决方案2】:

    我终于明白了。它必须与我的 mongodb 白名单一起使用。我只有我的 IP 地址,所以这是访问 dB 的唯一方法。感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 2014-05-28
      • 2021-11-30
      相关资源
      最近更新 更多