部署mongoDB heroku问题

时间:2019-06-28 19:38:25

标签: mongodb heroku deployment

为什么当我将自己的Mern应用程序部署到heroku时。它总是无法运行我的mongoDB?

2019-06-28T19:15:21.483892 + 00:00应用程序[web.1]:MongoNetworkError:第一次连接时无法连接到服务器[MongoNetworkError:连接5至///已关闭

我的代码是

const express = require('express');
const users = require('./server_utils/users');
const auth = require('./server_utils/auth');
const contacts = require('./server_utils/contacts');
const app = express();
require('dotenv').config();
const mongoose = require('mongoose');
const path = require('path');

const PORT = process.env.PORT || 8000



//this part below always fails to connect 
  once deployed to heroku....
 i cant discover mu MONGO_URI
 but its not trying connect to localHOST

mongoose.connect( process.env.MONGO_URI , {
     useNewUrlParser:true,
     useCreateIndex:true,
     useFindAndModify:false
}).then(() => console.log('MongoDb Connected...'))
.catch(err => console.log(err));


app.use(express.json({extended:false}))

app.use('/api/users', users );
app.use('/api/auth', auth );
app.use('/api/contacts', contacts );




app.use(express.static(path.join(__dirname, 'heroku_static')));

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname + '/heroku_static/index.html'));
});


app.listen(PORT);

0 个答案:

没有答案