Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Adding MongoDB to your Node App on Heroku

Unable to get my app to deploy to heroku. I am following along to the video 'User Authentication with Express'

I have a Node MongoDB application that I am trying to deploy to heroku. I have added mongolabs to my application, but I continue to get an "application error."

This is my app on GitHub: https://github.com/Missybur/Ping-Pong

Here is my 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 App

I have followed the following steps:

  • heroku login
  • heroku apps:create missy-pong
  • heroku addons:create mongolab
  • heroku git:remote -a missy-pong
  • git push heroku master

If anyone has any idea how to resolve this issue I would really appreciate it!