Heroku部署无法与MongoDb配合使用

2hh7jdfx  于 2022-11-13  发布在  Go
关注(0)|答案(2)|浏览(98)

我用ReactJS,NodeJS,MongoDb和Express创建了一个演示应用程序。尝试在heroku上部署。如果我不使用mongo,它工作正常,但一旦我引入mongo数据库。我得到错误不能GET /。我正在使用mongodb atlas。我需要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"
  }
}

过程文件
web:npm运行开发
已尝试Web:NPM也启动。

ctehm74n

ctehm74n1#

根据我有限的经验,我也遇到过同样的问题,结果是我忘了在Heroku上配置我的环境变量,所以我的MONGO_URI没有定义。如果不是这样,你可以使用Heroku CLI并从你的项目的根目录运行heroku logs --tail,也许可以看到更多关于发生了什么的信息。

ubof19bj

ubof19bj2#

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

上面的代码不正确。您必须更改< username >< password >用户名和密码中的和(都包含〈〉)!示例:

const CONNECTION_URL = process.env.MONGODB_URI || "mongodb+srv://kanechan25:kane02052409@cluster0.xzzno.mongodb.net/<dbname>?retryWrites=true&w=majority";

相关问题