0
我有一個Node MongoDB應用程序,我試圖部署到heroku。我在我的應用程序中添加了mongolab,但我仍然遇到「應用程序錯誤」。Heroku - 無法部署MongoDB節點應用程序
這是我在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
我跟隨以下步驟:
- heroku登錄
- Heroku的應用程序:創建小姐乒乓
- Heroku的插件:創建mongolab
- 的Heroku的git:遠程-a小姐乒乓
- 混帳推Heroku的主
- Heroku的開放
如果任何人有任何想法如何解決這個問題,我真的很感激它!