我有一个Node MongoDB应用程序,我正在尝试部署到heroku。我已将mongolab添加到我的应用程序中,但我仍然收到“应用程序错误。”
这是我在GitHub上的应用:GitHub
这是我的app.js:
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var app = express();
var port = process.env.PORT || 3000;
mongoose.connect(process.env.MONGOLAB_URI || 'mongodb://localhost/foobar');
// mongodb connection
mongoose.connect("mongodb://localhost:27017/pingpong");
var db = mongoose.connection;
// mongo error
db.on('error', console.error.bind(console, 'connection error:'));
// use sessions for tracking logins
app.use(session({
secret: 'treehouse loves you',
resave: true,
saveUninitialized: false,
store: new MongoStore({
mongooseConnection: db
})
}));
// make user ID available in templates
app.use(function (req, res, next) {
res.locals.currentUser = req.session.userId;
next();
});
// parse incoming requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// serve static files from /public
app.use(express.static(__dirname + '/public'));
// view engine setup
app.set('view engine', 'pug');
app.set('views', __dirname + '/views');
// include routes
var routes = require('./routes/index');
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('File Not Found');
err.status = 404;
next(err);
});
// error handler
// define as the last app.use callback
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
// listen on port 3000
app.listen(3000, function () {
console.log('Express app listening on port 3000');
});
In addition, here are my heroku logs:
2017-09-24T18:50:56.086372+00:00 app[web.1]: message: 'failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]' }
2017-09-24T18:50:56.086371+00:00 app[web.1]: name: 'MongoError',
这是我尝试部署的heroku链接:Heroku App
我已按照以下步骤操作:
如果有人知道如何解决这个问题我真的很感激!
答案 0 :(得分:0)
运行heroku config
并检查MONGOLAB_URI是否是正在配置的变量。
我有一个类似的问题,当我检查heroku config
时,我发现由heroku设置的mongodb uri是 MONGODB_URI 而不是 MONGOLAB_URI