node.js - 后端节点请求在 localhost 上工作,但在 Heroku 部署中不工作
问题描述
我一直在练习使用作为运动跟踪器的基本 MERN 应用程序部署到 Heroku。你可以在这里看到页面。现在 UI 正在工作,但对后端的每个请求都会遇到 503 错误。在花了三个小时谷歌搜索并尝试解决问题后,我想我会咨询 stackOverflow。
在我当前的项目中,我的所有东西都在我的本地主机上运行,但不是在 heroku 上。我可以取消与 MongoDB 的连接是问题,因为它在本地工作。现在,我的直觉是问题出在我的后端 package.json 文件、server.js 文件、procfile 或 config.js 中。
我仍在学习使用 Node,所以即使你不能告诉我答案,但可以告诉我应该采取哪些步骤来解决问题,我将不胜感激。
如果您想在 github 上查看我的完整代码,可以在这里查看。
这是我在 heroku 日志中收到的错误消息示例。
2020-09-01T13:24:28.665631+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/exercises/" host=exercise-tracker-deployment-3.herokuapp.com request_id=f2b3dfaa-3e08-449a-b3a4-453ca864cc0f fwd="129.137.144.10" dyno=web.1 connect=1ms service=30001ms status=503 bytes=0 protocol=https
这是我当前后端的 package.json
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "cd client && npm run build",
"install-client": "cd client && npm install",
"heroku-postbuild": "cd client && npm install && npm run build",
"start": "node server.js",
"client": "cd client && npm start",
"dev": "concurrently -n 'server,client' -c 'red,green' \"nodemon server.js\" \"npm run client\""
},
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongoose": "^5.10.0"
}
}
这是我的 server.js
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose')
require('dotenv').config();
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(express.json());
app.use(express.static('client/build'))
const uri = process.env.ATLAS_URI;
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true }
);
const connection = mongoose.connection;
connection.once('open', () => {
console.log("MongoDB database connection established successfully");
})
const exercisesRouter = require('./routes/exercises');
const usersRouter = require('./routes/users');
app.use('/exercises', exercisesRouter);
app.use('/users', usersRouter);
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});
这是我非常基本的procfile
web: npm start
这是我的 config.js
import dotenv from 'dotenv'
dotenv.config();
export const BACKEND_URL = process.env.NODE_ENV === 'development'? "http://locahost:5000" : "https://exercise-tracker-deployment-3.herokuapp.com/"
解决方案
我遇到了类似的问题,解决方案实际上是因为在 MongoDB Atlas -> Network Access -> IP Whitelist 中,我没有添加 0.0.0.0/0 作为新的 IP 地址。
添加后,代码将顺利运行。
还要检查您正在使用的集合和数据库是否已经在集群中创建,即使它们是空的。
推荐阅读
- python - 无法使用 python 2.7 和更新的 pyserial 3.4 设置 vpython(python 的视频)
- haskell - 使用 Applicative 和 Functor 的 Haskell 函数
- python - 在 Python 中拟合 sklearn.KNN 的内存错误
- javascript - 我无法在 AXIOS 中发出 GET 请求?
- javascript - 需要查找从哪个网站重定向
- node.js - 将 VAPID Push 订阅导入 Firebase 时预期的 OAuth 2 访问令牌
- c# - 如何知道从哪一点触发HttpClient任务取消
- linux - 'find' 找不到文件,可能是什么原因?
- python - 在 Keras 训练期间,tf.py_func 和 sklearn 的 AUC 不同
- supervisord - 是否可以为每个用户(目录)单独使用不同的主管(配置)?