[英]Heroku Deployment not working with MongoDb
I created one demo app with ReactJS, NodeJS, MongoDb and Express.我用 ReactJS、NodeJS、MongoDb 和 Express 创建了一个演示应用程序。 Trying to deploy on heroku.尝试在 heroku 上部署。 It works fine, if i dont use mongo, but as soon as i introduced mongo db.如果我不使用 mongo,它工作正常,但是一旦我引入了 mongo db。 I am getting error cannot GET /.我收到错误无法获取 /。 I am using mongodb atlas.我正在使用 mongodb 地图集。 Do I need heroku addon to use database?我需要 heroku 插件才能使用数据库吗?
server.js服务器.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 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"
}
}
procfile过程文件
web: npm run dev web:npm 运行开发
have tried web: npm start as well.也尝试过 web: npm 启动。
Just from my limited experience, I've had the same issue and it turned out I forgot to configure my environment variables on Heroku, so my MONGO_URI was undefined.仅根据我有限的经验,我遇到了同样的问题,结果我忘记在 Heroku 上配置我的环境变量,所以我的 MONGO_URI 未定义。 If not that, you can use the Heroku CLI and run heroku logs --tail
from the root of your project and might be able to see more about what's going on.如果不是这样,您可以使用 Heroku CLI 并从项目的根目录运行heroku logs --tail
并且可能能够看到更多关于正在发生的事情。
const CONNECTION_URL = process.env.MONGODB_URI || "mongodb+srv://<username>:<password>@cluster0.xzzno.mongodb.net/<dbname>?retryWrites=true&w=majority";
The upper code is incorrect.上面的代码不正确。 You have to change < username > and < password > (both include < >) by your usename and your password: Example:您必须通过您的用户名和密码更改 <username> 和 <password>(都包括 <>):示例:
const CONNECTION_URL = process.env.MONGODB_URI || "mongodb+srv://kanechan25:kane02052409@cluster0.xzzno.mongodb.net/<dbname>?retryWrites=true&w=majority";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.