简体   繁体   English

Heroku 503 服务不可用

[英]Heroku 503 Service Unavailable

I am currently building a simple CRUD app using ExpressJS, and host it on Heroku using free account.我目前正在使用 ExpressJS 构建一个简单的 CRUD 应用程序,并使用免费帐户将其托管在 Heroku 上。

The problem I ran into is:我遇到的问题是:

  • GET API for getting all items works on localhost, but show status 503 when hosting on Heroku;获取所有项目的 GET API 在 localhost 上工作,但在 Heroku 上托管时显示状态 503;
  • POST API for updating one item works on localhost, but same issue as GET API above;用于更新一项的 POST API 适用于 localhost,但与上述 GET API 的问题相同;
  • All 503 errors are after 30s of loading, this should be a setting from Heroku.所有 503 错误都是在加载 30 秒后出现的,这应该是 Heroku 的设置。

I do have other API end points that work on both local and Heroku server:我确实有其他适用于本地和 Heroku 服务器的 API 端点:

  • GET API for getting one item using ID GET API 用于使用 ID 获取一项

My guessing:我的猜测:

  • The issue should not be a code issue该问题不应该是代码问题
  • There is some issue when the code is deployed and Heroku cannot process this request部署代码时出现一些问题,Heroku 无法处理此请求

I tried to find some articles on the web but this seems hard to diagnose, anyone who has experience please let me know how I can solve this issue.我试图在网上找到一些文章,但这似乎很难诊断,任何有经验的人请告诉我如何解决这个问题。 Appreciate your comments.感谢您的评论。

My Mongoose Schema我的猫鼬模式

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);

2 end points that do not work 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 });
  }
});

The end point that works有效的终点

// 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 });
  }
});

My package.json for this express app我的这个快递应用程序的 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"
  }
}

EDIT: My index.js编辑:我的 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 + "...");
});

The root cause after checking is due to the access restriction from Heroku to Mongo atlas.检查后的根本原因是由于Heroku到Mongo atlas的访问限制。

After adding 0.0.0.0/0 for IP whitelist, I am able to get data from MongoDB.为 IP 白名单添加 0.0.0.0/0 后,我可以从 MongoDB 获取数据。

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

In my case, the issue was in package.json I installed two new packages yesterday and in my package.json:就我而言,问题出在 package.json 中,我昨天在 package.json 中安装了两个新包:

"engines": {
    "node": "12.16.0"
  },

I changed the version to the my current system version:我将版本更改为我当前的系统版本:

"engines": {
    "node": "14.5.0"
  },

this is how my 503 service unavailable error gone.这就是我的 503 服务不可用错误消失的方式。

This can be caused in several ways to the Node app.这可以通过多种方式对 Node 应用程序造成。

1- Make sure that you specify the right port as what heroku does is it runs our app on a dynamic port. 1- 确保您指定正确的端口,因为 heroku 所做的是它在动态端口上运行我们的应用程序。

const PORT = process.env.PORT || 3000;
app.listen(PORT, err => {
  if(err) throw err;
  console.log("%c Server running", "color: green");
});

as described in this comment https://stackoverflow.com/a/52992592/11887902如本评论所述https://stackoverflow.com/a/52992592/11887902

2- Make sure that you added the Procfile with 2-确保您添加了 Procfile

npm start

to start the application.启动应用程序。

3- If you are using Nodemon and you install it globally, just make sure that you install it locally too. 3- 如果您使用的是 Nodemon 并且您在全局安装它,请确保您也在本地安装它。

Finally, just have a look at the logs of the project to figure what heppen to make your project not in the service.最后,只需查看项目的日志,了解导致您的项目不在服务中的原因。

在此处输入图像描述

These cases happened to me and caused this error.这些情况发生在我身上并导致了这个错误。

I had the same problem and I was able to fix it by doing these two steps:我遇到了同样的问题,我可以通过执行以下两个步骤来解决它:

  1. Add node version to package.json file:将节点版本添加到 package.json 文件:
  "engines": {
    "node": "14.17.3"
  }
}
  1. I changed access settings in MongoDB, allowing access to the database from anywhere (basically, whitelisting 0.0.0.0/0 IP address)我更改了 MongoDB 中的访问设置,允许从任何地方访问数据库(基本上,将 0.0.0.0/0 IP 地址列入白名单)

我的问题是由于节点有时会忽略大小写敏感引起的,在我的controllers中,我有一个名为sessions.js的文件,但在导入路由时,我错误地将... = require('../controllers/Sessions') ,Nodemon 在我开发时运行没有问题,但是在 heroku 上部署时它崩溃了,所以我改为... = require('../controllers/sessions')现在它运行正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OpenShift:503服务不可用 - OpenShift: 503 Service Unavailable AWS 503服务暂时不可用 - AWS 503 Service Temporarily Unavailable AWS Lambda:503 服务不可用 - AWS Lambda: 503 Service Unavailable Heroku:加载资源失败:服务器响应状态为 503(服务不可用)/favicon.ico - Heroku: Failed to load resource: the server responded with a status of 503 (Service Unavailable) /favicon.ico 部署 MERN 应用程序后,Heroku 无法从数据库检索数据(给出 503(服务不可用)) - Heroku couldn't retrieve data from db (gives 503 (Service Unavailable)) after deployment MERN app 如何修复 503(服务不可用)? - How can I fix 503 (service unavailable)? 503 服务不可用响应。 如何使用 Heroku 使用服务器端代码中继 API? - 503 Service Unavailable response. How can I use Heroku to relay API's using server-side code? 我的应用程序在本地,所有路线上均正常运行,没有任何问题。 但是在Heroku上,所有员工路线都无法获得503服务 - My app works perfectly locally, all routes, with no issues. But on Heroku, all employees routes are getting a 503 service unavailable React-Heroku Postgress-Addon:GET http://app-name.herokuapp.com/any 503(服务不可用) - React-Heroku Postgress-Addon: GET http://app-name.herokuapp.com/any 503 (Service Unavailable) Google Cloud Run 返回错误 503 - 服务不可用 - Google Cloud Run is returning error 503 - Service Unavailable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM