Heroku 503 服务不可用

Pay*_*ang 2 heroku mongoose node.js express

我目前正在使用 ExpressJS 构建一个简单的 CRUD 应用程序,并使用免费帐户将其托管在 Heroku 上。

我遇到的问题是:

  • 用于获取所有项目的 GET API 在本地主机上工作,但在 Heroku 上托管时显示状态 503;
  • 用于更新一项的 POST API 在本地主机上工作,但与上面的 GET API 相同;
  • 所有 503 错误都是在加载 30 秒后出现的,这应该是 Heroku 的设置。

我确实有其他可以在本地和 Heroku 服务器上运行的 API 端点:

  • 使用 ID 获取一件物品的 GET API

我的猜测:

  • 问题不应该是代码问题
  • 部署代码时出现一些问题,Heroku 无法处理此请求

我试图在网上找到一些文章,但这似乎很难诊断,任何有经验的人请告诉我如何解决这个问题。感谢您的评论。

我的猫鼬架构

const mongoose = require("mongoose");

const ThoughtSchema = mongoose.Schema({
  title: {
    type: String,
    required: true
  },
  content: {
    type: String,
    required: true
  },
  date: {
    type: Date,
    default: Date.now
  }
});

module.exports = mongoose.model("Thought", ThoughtSchema);
Run Code Online (Sandbox Code Playgroud)

2个不起作用的终点

// Returns all thoughts
router.get("/", async (req, res) => {
  try {
    const thought = await Thought.find();
    res.json(thought);
  } catch (err) {
    res.json({ message: err });
  }
});

// Submit a post
router.post("/", async (req, res) => {
  const thought = new Thought({
    title: req.body.title,
    content: req.body.content
  });

  try {
    const savedThought = await thought.save();
    res.json(savedThought);
  } catch (err) {
    res.json({ message: err });
  }
});
Run Code Online (Sandbox Code Playgroud)

有效的终点

// Specific thought
router.get("/:thoughtId", async (req, res) => {
  try {
    const thought = await Thought.findById(req.params.thoughtId);
    res.json(thought);
  } catch (err) {
    res.json({ message: err });
  }
});
Run Code Online (Sandbox Code Playgroud)

我的 package.json 用于这个快速应用

{
  "name": "my-thoughts-app",
  "version": "0.1.0",
  "description": "An app to records user's thoughts",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/PayneTang/my-thoughts-app.git"
  },
  "author": "Payne Tang",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/PayneTang/my-thoughts-app/issues"
  },
  "homepage": "https://github.com/PayneTang/my-thoughts-app#readme",
  "dependencies": {
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "mongoose": "^5.8.11"
  },
  "devDependencies": {
    "typescript": "^3.7.5"
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑: 我的 index.js

const express = require("express");
const path = require("path");
const app = express();
const mongoose = require("mongoose");
const thoughtRoute = require("./routes/thought");
require("dotenv").config();

console.log(process.env);

// Mongoose settings
mongoose.connect(
  process.env.DB_CONNECTION,
  { useNewUrlParser: true, useUnifiedTopology: true },
  () => {
    console.log("Connected to DB!");
  }
);

app.use(express.json());
app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Methods", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  next();
});

app.use("/api/thought", thoughtRoute);
app.get("/api/test", (req, res) => {
  res.send("hi");
});

// Serve client side
app.use(express.static(path.join(__dirname, "client/build")));
app.use(express.static(path.join(__dirname, "client/public")));
// app.get("*", (req, res) => {
//   res.sendFile(path.join(__dirname, "client/build/index.html"));
// });

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
  console.log("Listening on port " + PORT + "...");
});
Run Code Online (Sandbox Code Playgroud)

Pay*_*ang 5

检查后的根本原因是由于从Heroku到Mongo图集的访问限制。

为 IP 白名单添加 0.0.0.0/0 后,我可以从 MongoDB 获取数据。

参考:https : //docs.atlas.mongodb.com/security-whitelist/#add-whitelist-entries

  • 添加 0.0.0.0/0 作为 IP 白名单后,您可能需要使用以下命令重新启动 heroku: `heroku restart -a app_name` (2认同)