我正在尝试使用heroku部署MERN堆栈应用程序,但是在部署过程中我遇到了问题。
我收到以下错误:
MongoNetworkError在首次连接[MongoNetworkError:到cluster0-shard-00-00-yyfkd.mongodb.net:27017的连接5时无法连接到服务器[cluster0-shard-00-00-yyfkd.mongodb.net:27017]关闭]
我设置了所需的环境变量,但不断出现相同的错误。
我尝试使用此视频https://www.youtube.com/watch?v=qXIG8iKO7Fo&t=318s中介绍的mlab插件,该插件无效。
我尝试从以下位置更改DATABASE环境变量:
mongodb + srv :: @ cluster0-yyfkd.mongodb.net/test?retryWrites=true&w=majority
至:
mongodb + srv://:@ cluster0-yyfkd.mongodb.net /。
这也不起作用。
我将heroku cli升级到了最新版本,该版本也不起作用。
我尝试多次部署该应用程序,因为我读到它在某些情况下可以正常工作(我现在已经将该应用程序部署了20多次)。
我的互联网连接正常。
我当前正在运行以下版本的node和npm:
"engines": {
"node": "10.15.3",
"npm": "6.10.3"
},
我的package.json当前看起来像这样:
{ "name": "clear", "version": "1.0.0", "description": "", "main": "server.js", "scripts": {
"client": "cd client && npm start",
"server": "nodemon server.js",
"build": "cd client && npm run build",
"dev": "concurrently --kill-others-on-fail \"npm run server\" \"npm run client\"",
"start": "node server.js",
"heroku-postbuild": "cd client && npm install && npm install --only=dev --no-shrinkwrap && npm run build" }, "author": "", "license": "ISC", "dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"compression": "^1.7.4",
"concurrently": "^5.1.0",
"cookie-parser": "^1.4.4",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-mongo-sanitize": "^1.3.2",
"express-rate-limit": "^5.0.0",
"helmet": "^3.21.1",
"hpp": "^0.2.2",
"html-to-text": "^5.1.1",
"jsonwebtoken": "^8.5.1",
"mongodb": "^3.5.4",
"mongoose": "^5.7.1",
"morgan": "^1.9.1",
"multer": "^1.4.2",
"ndb": "^1.1.5",
"nodemailer": "^6.3.0",
"npm": "^6.4.1",
"path": "^0.12.7",
"pug": "^2.0.4",
"slugify": "^1.3.5",
"stripe": "^7.9.1",
"validator": "^11.1.0",
"xss-clean": "^0.1.1" }, "engines": {
"node": "10.15.3",
"npm": "6.10.3" }, "devDependencies": {
"cross-env": "^7.0.0",
"concurrently": "^4.0.1" } }
这是我在server.js中运行的代码:
const DB = process.env.DATABASE.replace(
'<PASSWORD>',
process.env.DATABASE_PASSWORD
);
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
})
.then(() => console.log('DB connection successful!'));
const port = process.env.PORT || 4000;
const server = app.listen(port, () => {
console.log(`App running on port ${port}...`);
});
任何帮助,感觉或方向将不胜感激!