0

这是我的 mongodb atlas 连接代码,它显示连接成功,但后来当我单击某个选项卡时,应用程序崩溃。我正在通过 heroku 部署应用程序,并且我给出了以下错误。应用程序在 localhost 上运行良好,但在 heroku 网站上崩溃。它有时允许并打开一些页面,但在那之后立即崩溃,我尝试在 heroku 网站上修复环境变量,但这没有帮助,所以我切换到只使用通过变量的 mongo 连接链接。任何帮助表示赞赏。我发现服务器在 30 秒后抛出无法连接的错误,但仍然找不到导致错误的原因

if (process.env.NODE_ENV !== "production") {
    require('dotenv').config();
}

const sanitize = require('express-mongo-sanitize');
// const dbURL = process.env.DB_URL;
const session = require('express-session');
const MongoStore = require('connect-mongodb-session')(session);
const dbURL = "mongodb+srv://RohanGupta:****************@*****.****.mongodb.net/oddevegame?retryWrites=true&w=majority";

mongoose.connect(dbURL, {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true,
    useFindAndModify: false
})
    .then(() => {
        console.log("Connected Mongo Successfully")
    })
    .catch(err => {
        console.log("Uh Oh couldnt connect mongo!!")
        console.log(err);
    })


app.use(methodOverride('_method'));
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, '/public')));
app.use(express.static(path.join(__dirname, '/resources')));



app.use(session({
    store: new MongoStore({
        url: dbURL,
        touchAfter: 24 * 3600
    }),
    name: "blah",
    secret: 'this is secret',
    resave: false,
    saveUninitialized: false,
    cookie: {
        httpOnly: true,
        // secure: true,
        expires: Date.now() + 1000 * 60 * 60 * 24 * 7,
        maxAge: 1000 * 60 * 60 * 24 * 7
    }
}));

2021-06-20T07:27:23.462279+00:00 heroku[web.1]: State changed from starting to up
2021-06-20T07:27:23.230556+00:00 app[web.1]: Listening on PORT 4000
2021-06-20T07:27:23.502833+00:00 app[web.1]: Connected Mongo Successfully
2021-06-20T07:27:53.221633+00:00 app[web.1]: /app/node_modules/mongodb/lib/utils.js:698
2021-06-20T07:27:53.221654+00:00 app[web.1]:           throw error;
2021-06-20T07:27:53.221655+00:00 app[web.1]:           ^
2021-06-20T07:27:53.221655+00:00 app[web.1]: 
2021-06-20T07:27:53.221656+00:00 app[web.1]: Error: Error connecting to db: connect ECONNREFUSED 127.0.0.1:27017
2021-06-20T07:27:53.221656+00:00 app[web.1]:     at /app/node_modules/connect-mongodb-session/index.js:78:17 



4

0 回答 0