3

I am having a hard time to deploy my node.js application to Heroku because I am having issues with my mongo.db database..

I saw this approach in a tutorial and I really like it. it looks very clean and sits in the config.js folder so its separated from the actual application..

module.exports = {
  development: {
    root: rootPath,
    db: 'mongodb://localhost/minidatabase'
  },
  test: {
    root: rootPath,
    db: 'mongodb://localhost/minidatabase'
  },
  staging: {
    root: rootPath,
    db: process.env.MONGOLAB_URI
  },
  production: {
    root: rootPath,
    db: process.env.MONGOLAB_URI
  }
}

and I have this in my app.js file

var env = process.env.NODE_ENV || 'development'
  , config = require('./config/config')[env]
  , mongoose = require('mongoose')


mongoose.connect(config.db)

When I change mongodb://localhost/minidatabase in the development object in my config.js file with my MONGOLAB_URI heroku works fine, but this time I am having problems in my local server. I guess the whole point of specifying different configurations is not to change the URI every time the environment switches from development to production, but it seems like my application just assumes its on production mode all the time..

1
  • is my question unclear ? Commented Jul 30, 2013 at 3:51

1 Answer 1

4

It's probably better to create a local .env file and use Foreman (installed with the Heroku Toolbelt) to start your application.

You should put this in a local .env file:

DATABASE_URI=mongodb://localhost/minidatabase

and edit your source code to reference the database as:

mongoose.connect(process.env.DATABASE_URI)

Then start you app using:

foreman start

If you don't know how to install foreman, have a look at https://toolbelt.herokuapp.com/ Also, remember to run the following command to set the URI in Heroku:

heroku config:set DATABASE_URI=[the mongolab URI goes here]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.