【问题标题】:Heroku Deployment not working with MongoDbHeroku 部署不适用于 MongoDb
【发布时间】:2021-04-28 08:02:10
【问题描述】:

我使用 ReactJS、NodeJS、MongoDb 和 Express 创建了一个演示应用程序。试图在heroku上部署。如果我不使用 mongo,它工作正常,但是一旦我引入了 mongo db。我收到错误无法获取 /。 我正在使用 mongodb 地图集。我需要heroku插件才能使用数据库吗?

server.js

// Import dependencies
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
const MongoClient = require("mongodb").MongoClient;
const ObjectId = require("mongodb").ObjectID;
const mongodb = require('mongodb');
const fs = require('fs');
const moment = require("moment");

require('dotenv').config();

const CONNECTION_URL = process.env.MONGODB_URI || "mongodb+srv://<username>:<password>@cluster0.xzzno.mongodb.net/<dbname>?retryWrites=true&w=majority";
const DATABASE_NAME = "DBNAME";
const port = process.env.PORT || 5000;


const app = express();

// Set our backend port to be either an environment variable or port 5000

// This application level middleware prints incoming requests to the servers console, useful to see incoming requests
app.use((req, res, next) => {
    console.log(`Request_Endpoint: ${req.method} ${req.url}`);
    next();
});

// Configure the bodyParser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

// Configure the CORs middleware
app.use(cors());

app.get("/test/", (request, response) => {
    response.send({"name":"Hello Test!!!"});
});





var database, userSignUp;

app.listen(port, () => {
    MongoClient.connect(CONNECTION_URL, { useNewUrlParser: true }, (error, client) => {
        if(error) {
            throw error;
        }
        database = client.db(DATABASE_NAME);
        userSignUp = database.collection("UserData");
        
       
        console.log("Connected to `" + DATABASE_NAME + "`!");
       
    
    });
})

package.json

{
  "name": "testproject",
  "version": "1.0.0",
  "description": "Learning Deployment",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "client": "cd client && npm start",
    "server": "nodemon server.js",
    "dev": "concurrently --kill-others-on-fail \"npm run client\" \"npm run server\"",
    "client:build": "cd client && npm run build"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Username/TestProject.git"
  },
  "author": "Ankita Jaiswal",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/Username/TestProject/issues"
  },
  "homepage": "https://github.com/Username/TestProject#readme",
  "dependencies": {
    "body-parser": "^1.19.0",
    "concurrently": "^5.3.0",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "nodemon": "^2.0.7",
    "moment": "^2.29.1",
    "mongodb": "^3.6.3",
    "mongoose": "^5.11.8"
  }
}

过程文件

网络:npm 运行开发

也尝试过 web: npm start。

【问题讨论】:

    标签: node.js reactjs mongodb heroku deployment


    【解决方案1】:

    根据我有限的经验,我遇到了同样的问题,结果我忘记在 Heroku 上配置我的环境变量,所以我的 MONGO_URI 未定义。如果不是这样,您可以使用 Heroku CLI 并从项目的根目录运行 heroku logs --tail,或许能够看到更多关于正在发生的事情。

    【讨论】:

    • 它不是未定义的。我只收到错误“无法获取 /”
    • 你用的是heroku的mlab或者objectrocket插件吗?或者有任何heroku的付费帐户?
    • 我应该更具体。我得到的错误并没有说我的环境变量是未定义的,但我怀疑因为它是未定义的,我的应用程序崩溃了,我也得到了一个“无法获取”错误。我使用了标准免费层 MongoDB Atlas 和标准免费层 Heroku。没有插件。此外,尝试在 Heroku 配置中删除/添加 MONGO_URI 变量周围的引号。我忘记了,但我认为您不需要引号。确保没有空格等。
    • 能否分享一下server.js、目录结构、package.json和mongo_uri cmd。在我的邮件 ankitajaiswal2890@gmail.com
    • 此项目已部署并正在运行。相关代码在这里。 github.com/Perrottarichard/pizzapizza-server/blob/master/…
    猜你喜欢
    • 2018-12-18
    • 2021-06-03
    • 2018-10-10
    • 2020-02-19
    • 2022-01-24
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    相关资源
    最近更新 更多