无法从Heroku上部署的应用程序连接到Mongo Atlas

时间:2019-08-08 18:12:41

标签: node.js mongodb heroku mongoose

我的项目在本地运行时可以完美连接到mongoAtlas,但是在heroku上部署时则不能连接到mongoAtlas。我已经在heroku上设置了所有环境变量。我还将mongo Atlas上的白名单IP设置为0.0.0.0/0,以便可以从任何地方访问它。但是问题仍然存在。

  

app.js

var express  = require("express");
var app      = express();
var mongoose =require("mongoose");
var passport =require("passport");
var localStrategy = require("passport-local");
var passportLocalMongoose = require("passport-local-mongoose");
var methodOverride =require("method-override");
var flash =require("connect-flash");

var Comment=require("./models/comment");
var Post = require("./models/post");
var User = require("./models/user");

var postRoutes = require("./routes/posts");
var profileRoutes = require("./routes/profile");
var authRoutes = require("./routes/auth");
var indexRoutes = require("./routes/index");
var commentRoutes = require("./routes/comments");

require('dotenv').config()
app.use(express.static(__dirname +"/public"));
app.use(methodOverride("_method"));
app.locals.moment = require('moment');

app.set("view engine", "ejs");
app.use(flash());

//for jquery
var jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM();
const { document } = (new JSDOM('')).window;
global.document = document;
var $ = require("jquery")(window);

var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended: true}));

var cloudinary = require('cloudinary');
cloudinary.config({ 
  cloud_name: 'ghostx61', 
  api_key: process.env.CLOUDINARY_API_KEY, 
  api_secret: process.env.CLOUDINARY_API_SECRET
});

//DATABASEURL variable for mongoAtlas
mongoose.connect(process.env.DATABASE_URL || "mongodb://localhost:27017/mongoDemo_v7");


//passport config
app.use(require("express-session")({
    secret: "dsadsfjfgjhfghfdjhfgjgdhjkg",
    resave: false,
    saveUninitialized: false
}));

app.use(passport.initialize());
app.use(passport.session());
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

app.use(function(req, res, next){
    res.locals.message= req.flash("error");
    next();
});

app.use(profileRoutes);
app.use(authRoutes);
app.use(postRoutes);
app.use(indexRoutes);
app.use(commentRoutes);

//IP variable for heroku deploy
app.listen(process.env.PORT || 3000, function(){
    console.log("Server running on port 3000");
});
  

在执行heroku日志--tail命令时显示错误

2019-08-08T16:37:25.986574+00:00 app[web.1]:
2019-08-08T16:37:28.077231+00:00 app[web.1]: Warning: connect.session() MemoryStore is not
2019-08-08T16:37:28.077269+00:00 app[web.1]: designed for a production environment, as it will leak
2019-08-08T16:37:28.077271+00:00 app[web.1]: memory, and will not scale past a single process.
2019-08-08T16:37:28.084568+00:00 app[web.1]: (node:23) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version.
To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
2019-08-08T16:37:28.085200+00:00 app[web.1]: Server running on port 3000
2019-08-08T16:37:28.254212+00:00 app[web.1]: (node:23) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [cluster0-shard-00-01-bd7rs.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to cluster0-shard-00-01-bd7rs.mongodb.net:27017 closed]
2019-08-08T16:37:28.254215+00:00 app[web.1]: at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:431:11)
2019-08-08T16:37:28.254217+00:00 app[web.1]: at Pool.emit (events.js:198:13)
2019-08-08T16:37:28.254219+00:00 app[web.1]: at connect (/app/node_modules/mongodb-core/lib/connection/pool.js:557:14)
2019-08-08T16:37:28.254221+00:00 app[web.1]: at callback (/app/node_modules/mongodb-core/lib/connection/connect.js:109:5)
2019-08-08T16:37:28.254222+00:00 app[web.1]: at runCommand (/app/node_modules/mongodb-core/lib/connection/connect.js:129:7)
2019-08-08T16:37:28.254224+00:00 app[web.1]: at Connection.errorHandler (/app/node_modules/mongodb-core/lib/connection/connect.js:321:5)
2019-08-08T16:37:28.254226+00:00 app[web.1]: at Object.onceWrapper (events.js:286:20)
2019-08-08T16:37:28.254227+00:00 app[web.1]: at Connection.emit (events.js:198:13)
2019-08-08T16:37:28.254229+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:350:12)
2019-08-08T16:37:28.254231+00:00 app[web.1]: at Object.onceWrapper (events.js:286:20)
2019-08-08T16:37:28.254233+00:00 app[web.1]: at TLSSocket.emit (events.js:198:13)
2019-08-08T16:37:28.254234+00:00 app[web.1]: at _handle.close (net.js:606:12)
2019-08-08T16:37:28.254237+00:00 app[web.1]: at TCP.done (_tls_wrap.js:388:7)
2019-08-08T16:37:28.254299+00:00 app[web.1]: (node:23) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
2019-08-08T16:37:28.254365+00:00 app[web.1]: (node:23) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

2 个答案:

答案 0 :(得分:0)

我在mongoose.connect()方法中添加了错误处理,它可以正常工作。请注意,此代码只处理最初的连接错误,这首先是问题。要了解更多信息,请访问here

  

代码

mongoose.connect(process.env.DATABASE_URL || "mongodb://localhost:27017/mongoDemo_v7", { useNewUrlParser: true }).
  catch(error => handleError(error));

答案 1 :(得分:0)

要允许访问heroku应用,您需要打开allow access from anywhere。请参阅此处的步骤MongoDB IP Whitelist