4

I have a Node.js app using Express that stores data in a mongoDB (locally).

It's now pushed to heroku successfully on a cedar stack and the server is running. I added the mongohq addon through the terminal.

Now... how do I connect that mongoDB through mongohq to my application to start using it??? I don't see a tutorial for this anywhere online. If you can point me to one or get me started on where to add the configuration, I would greatly appreciate!

Thanks much.

Update:

I've tried the following (with real values for MYPASSWORD and MYDBNUMBER:

in routes.js

var db = mongoose.connect('mongodb://heroku:<MYPASSWORD>@staff.mongohq.com:10049/<MYDBNUMBER>');

in my schema.js (also tried using the heroku

var mongoose = require('mongoose');
mongoose.connect('mongodb://heroku:<MYPASSWORD>@staff.mongohq.com:10049/<MYDBNUMBER>');

my package.json

{
"name": "NAME"
, "version": "0.0.1"
, "dependencies": {
  "express": "2.4.6"
, "connect": "1.7.1"
, "stylus": ">= 0.0.1"
, "mongodb": ">= 0.9.6-7"
, "mongoose": ">= 2.0.0"
, "ejs": ">=0.4.3"
}
}

Right now, only the root '/' GET is successful. Actually, if I try /page/ANYTHING I can successfully get a rendered 500 Error page that I made to handle attempts to get 'pages' that haven't been created... That seems kind of weird to me. Everything else gives me an Internal Server Error.

ALSO, if i go to mongohq and open my database there, a collection for my model pages has been created and the indexes for uniqueness have been created, even tho I haven't been able to access the pages to create or view the model in my app...

happy to provide any other info... so stuck.

tuddy
  • 1,824
  • 4
  • 31
  • 35

1 Answers1

2

We are using heroku against MongoHQ but in our case we created the MongoHQ account on our own so our solution is a bit different. Now, we are using the Redis plugin and in that case we connect to Redis using the heroku env variables. When you create a new plugin, Heroku adds a bunch of env variables that you can access from your node.js app easily doing this:

process.env.NAME_OF_VAR

In the case of Redis to go the name of the var is REDISTOGO_URL so our code looks like

process.env.REDISTOGO_URL

For MongoHQ the env variable seems to be MONGOHQ_URL

You may need to do some parsing. Heroku have some documentation for Ruby here http://devcenter.heroku.com/articles/mongohq#using_the_mongo_ruby_driver that should help you to get it going.

With respect to where to do this in an express app. We are doing all the setup on app.js that is our entry point in the app.

Hope this helps.

theprogrammer
  • 2,724
  • 1
  • 18
  • 13
  • Hey thanks a lot for your input. I'll give this a shot in the next couple days and report back. – tuddy Oct 03 '11 at 17:20
  • 1
    Yeah, just not getting through with this thing! Is there anything I need to other than adding the variable in my main file `mongoose.connect(process.env.MONGOHQ_URL);` and in the schema `mongoose.connect(process.env.MONGOHQ_URL);`? I did the add on and the app works locally. The logs aren't really saying much, I'm just basically getting 500's when i try to do anything. I get `Internal Server Error` for most things except some urls that I have custom error message for will show that custom 500 view... so it's kinda working, i'm pretty sure it's just not accessing the DB. – tuddy Oct 06 '11 at 23:47
  • Ok, pretty sure it's a route thing, looks like I can access the DB fine actually. This answer helped. Thanks. – tuddy Oct 07 '11 at 17:09