无法从Heroku连接到Mongolab

时间:2018-10-18 17:04:19

标签: heroku

部署到heroku时出现以下错误。我已经阅读了文档,但不知道该怎么做:

{ MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]

是的,我确实运行过heroku插件:创建mongolab -a并在heroku网站上拥有配置变量。这是我的服务器文件:

var express = require('express')
var app = express()
var morgan = require("morgan")
var mongoose = require('mongoose')
require('dotenv').config()
const PORT = process.env.PORT || 8081
const rateRoutes = require('./routes/rateRouter')
const path = require("path")

app.use(morgan('dev'))
app.use(express.json())
app.use(express.static(path.join(__dirname, "client", "build")))


//connect to mongolab
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/rate', { useNewUrlParser: true})
    .then(()=>console.log('Successfully connected to mongoDB'))
    .catch(err=>console.log(err))

app.use('/last', rateRoutes)
app.use('/', rateRoutes)

app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
})

app.listen(PORT, () => {
console.log("starting")
})

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我认为您在连接线上遇到的问题,您确定该链接为:
mongodb://本地主机:27017 / rate
是从mongolab来的? 这是本地数据库,但heroku没有访问权限,我认为mongolab提供的用于全局连接数据库的链接将有所不同。