s_curry_s
s_curry_s

Reputation: 3432

Node.js Application with Mongo.db on Heroku

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..

Upvotes: 3

Views: 1545

Answers (1)

jmike
jmike

Reputation: 352

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]

Upvotes: 4

Related Questions