Node.js - Connecting to MongoDB using MongoHQ on Heroku

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

Now he successfully clicked on the geroku on the cedar stack and the server is working. I added the mongohq terminal through the terminal.

Now ... how to connect this mongoDB via mongohq to my application to start using it ??? I do not see a tutorial for this anywhere on the Internet. If you can point me to one or start me with where to add the configuration, I would really appreciate it!

Thank you very much.

Update:

I 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 hero

 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 root '/' GET is successful. In fact, if I try / page / ANYTHING, I can successfully get the displayed page of 500 errors that I made to handle attempts to get "pages" that were not created ... This seems strange to me. Everything else gives me an internal server error.

ALSO, if I go to mongohq and open my database there, a collection was created for my models, and indexes for uniqueness were created, even if I could not access the pages to create or view the model in my application ...

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

+4
source share
1 answer

We use a hero against MongoHQ, but in our case we created our own MongoHQ account, so our solution is slightly different. Now we use the Redis plugin, in which case we connect to Redis using the hero enk variables. When you create a new plugin, Heroku adds a bunch of env variables that you can get from your node.js application, this makes it easy:

 process.env.NAME_OF_VAR 

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

 process.env.REDISTOGO_URL 

For MongoHQ, the env variable seems MONGOHQ_URL

You may need to perform parsing. Heroku has documentation for Ruby here http://devcenter.heroku.com/articles/mongohq#using_the_mongo_ruby_driver , which should help you with this.

Regarding where to do it in the express application. We do all the settings on app.js, which is our entry point to the application.

Hope this helps.

+2
source

All Articles