【问题标题】:Back end Node requests work on localhost but not in Heroku deployment后端节点请求在 localhost 上工作,但在 Heroku 部署中不工作
【发布时间】:2020-12-20 15:20:30
【问题描述】:

我一直在练习使用作为运动跟踪器的基本 MERN 应用程序部署到 Heroku。你可以看到页面here。现在 UI 正在工作,但对后端的每个请求都会遇到 503 错误。在花了三个小时谷歌搜索并尝试解决问题后,我想我应该咨询 stackOverflow。

在我当前的项目中,我的所有内容都在本地主机上运行,​​但在 heroku 中却没有。我可以取消与 MongoDB 的连接是问题,因为它在本地工作。现在,我的直觉是问题出在我的后端 package.json 文件、server.js 文件、procfile 或 config.js 中。

我仍在学习使用 Node,所以即使你不能告诉我答案,但可以告诉我应该采取哪些步骤来解决问题,我将不胜感激。

如果你想在 github 上看到我的完整代码,你可以看到它here

这是我在 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}`);
    });

这是我非常基本的过程文件

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/" 

【问题讨论】:

    标签: node.js mongodb heroku


    【解决方案1】:

    我也遇到过类似的问题,解决方法其实是因为在 MongoDB Atlas -> Network Access -> IP Whitelist 中,我没有添加 0.0.0.0/0 作为新的 IP 地址。

    添加后,代码运行流畅。

    还要检查您正在使用的集合和数据库是否已经在集群中创建,即使它们是空的。

    【讨论】:

    • 我已经犯了大约六次这样的错误。不幸的是,我刚刚检查过,这不是我在这个项目上的问题。
    • @David Reke 尝试清除脚本并只保留启动、heroku-postbuild 和测试脚本
    • 我试了一下,还是没有成功:(
    • 你在前端使用 react 吗?如果是,请分享发出 fetch 或 axios 请求的代码。也许在您的前端,您正在向 localhost 而不是 heroku 发送请求。
    • 这是我使用 axios 的请求之一的示例: componentDidMount() { axios.get(BACKEND_URL +'users/') .then(response => { if (response.data.length > 0) { this.setState({ users: response.data.map(user => user.username), username: response.data[0].username }); } }) .catch((error) => { 控制台.log(error); }) }
    猜你喜欢
    • 2022-01-15
    • 2012-04-30
    • 2020-08-23
    • 2016-01-20
    • 1970-01-01
    • 2016-07-24
    • 2020-01-09
    • 2020-10-16
    • 1970-01-01
    相关资源
    最近更新 更多