[英]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:我遇到的问题是:
I do have other API end points that work on both local and Heroku server:我确实有其他适用于本地和 Heroku 服务器的 API 端点:
My guessing:我的猜测:
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:我遇到了同样的问题,我可以通过执行以下两个步骤来解决它:
"engines": {
"node": "14.17.3"
}
}
我的问题是由于节点有时会忽略大小写敏感引起的,在我的controllers
中,我有一个名为sessions.js
的文件,但在导入路由时,我错误地将... = require('../controllers/Sessions')
,Nodemon 在我开发时运行没有问题,但是在 heroku 上部署时它崩溃了,所以我改为... = require('../controllers/sessions')
现在它运行正常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.