keep separate the local database for local dev and nodejutsu database URL for nodejitsu app? - node.js

When I'm developing on my local box I use mongodb://localhost/Data as my connection string when connecting to mongoDB that's running locally.
But then when I upload/update my local app to nodejitsu I have to change the url to the one provided by nodejitsu mongodb://nodejitsu_XXX:xyxyxyxy.mongolab.com:29950/nodejitsu_XXX_nodejitsudb12121212
How do I switch between my localhost mongoDB connection URL and the one provided by Nodejitsu? I don't wanna keep changing it and committing the changes everytime I want to test out my local changes in the cloud...

I use nconf https://github.com/flatiron/nconf , you can easily change between diferent configurations

Related

Procedure for creating a database locally using mongodb

I've connected to mLab using MongoClient.connect, but now I want to create a local database rather than use mLab. I've tried changing the URL to "mongodb://localhost:27017/mydb" to create a database locally as suggested, but I'm getting a failed to connect to server error. Is there anything I should install? Or am I just missing something?
First, install your mongoDB server:
https://docs.mongodb.com/manual/installation/
After that, you should be able to start your local mongodb server:
sudo service mongod start //in case of linux
Here the docs to start the mongodb server
Then, you will type mongo and the client command line will start.
And when typing use yourDatabase, mongo will create the database if
it does not exist.
Then, your server will be up, and the database created. Anyway, check the docs if you need the guides for windows or other systems.
First you need to understand how server works and mlab works. mLab is a company who provide database as a services. Means you no need to handle server hurdles, they take care server , backups , infrastructure etc. They provide mongodb server , You just need to create database on mongodb server's and use them.
Now comes to main points.
If you want use mongodb on your local system then you first need to install mongodb on your local system and then connect it.

Connecting to a mongoDB with a TCP prefix on a NodeJS application

I created a NodeJS app that connects with a MongoDB and it seems to work locally.
Now, after hosting the NodeJS application on a remote machine, I am trying to connect to a mongoDB that was already created on that machine. When I print out some of the environment variables, the only one I see of relevance seems to be :
MONGODB_PORT: 'tcp://172.30.204.90:27017',
I tried connecting like I usually do with
mongoose.connect('mongodb://localhost:27017/metadata/')
and replacing it with mongoose.connect('tcp://172.30.204.90:27017/metadata') but I get an error that says my URI needs to start with 'mongodb'.
So I tried replacing it with mongoose.connect('mongodb://172.30.204.90:27017/metadata') and it no longer throws any error. But on the MongoDB side I don't see any new connections happening and my app does not start up on the machine. What should I be putting in the URI?
Your URI should indeed start with mongodb:
mongoose.connect('mongodb://username:password#host:port/database?options...');
See this page for more information: https://docs.mongodb.com/manual/reference/connection-string/
Did you try to connect to the database from cli? Or Telnet to see that connection isn't blocked.

MongoLab DB is not responding when using with localhost

I have an Heroku web applicaiton based on MongoLab MongoDB.
Accessing the site via heroku production site works and I see the site is alive.
My development environment is configured to work with the production DB.When I try to connect I get on:
mongoose.connection.on('error', function(err) {
...
});
And err.message = "connect ETIMEDOUT 54.159.67.179:61188"
How should I continue?
If you click through the resources section of the heroku dashboard and then open your mlab resource it should take you to the mlab home.
The first bit of information should show you how to connect to your database either though the mongo shell or using the standard URI. If you have mongo installed locally can you connect to the remote database this way? If not then it points to network issues. Are you accessing this from behind a corporate proxy? Mongo by default uses port 27017 which isn't open on most corporate proxies. If this is the case then I would speak to your network administrator. I know that at my company we aren't allowed to use this port, but then we don't use cloud infrastructure (yet). Good luck!
I faced the same issue. Solution for me ended up that the environment variable (stored in a .env file in my app) had an old "configvar" (Heroku terminology) and:
go to the desired app Settings tab in Heroku
click the Reveal ConfigVars button
copy the MONGODB_URI value
paste the URI into the .env file (or wherever you store)
That was it, back in business.

Changes need to run mongoose on server instead of localhost?

What are the changes I need to run my app on server instead of localhost
mongoose.connect('mongodb://mongoServerIP/dbName)
Since you are running your mongo in your local, you are using localhost. I am not familiar with heroku server, but in general case, you first need to make sure mongo db is running on your heroku server and than you need to put that IP instead of localhost in the above command.
Please refer http://mongoosejs.com/docs/connections.html in case you need login credentials to access the server or any other reference.

Connecting to Redis databases via Node in Heroku

About Heroku: App is Nodejs, Express, WebSockets ...
I'm testing an app out on a small free Heroku instance.
I have my own Redis database instances through OpenRedis and RedisToGo.
Those have not been provisioned through Heroku addons but directly from the providers.
I connect to them fine when running locally. What special do I need to do to connect to them through Heroku?
I was kind of hoping to avoid special Heroku-specific environment variables, but am happy to use them if necessary.
But I definitely need to avoid the necessity of provisioning the Redis addons through Heroku.
Again, I have already my database instances provisioned directly with the relevant providers.
Everything works fine locally under NODE_ENV=production npm start, which indicates to my app that it should connect to the remote Redis instances (RedisToGo and/or OpenRedis) instead of the local Redis server.
I'm using Redis for at least three things:
Session store :: Connect-Redis :: This works fine locally (app server running locally but connecting to remote Redis instance) and also seems to work on Heroku-- I've checked by flushing the database and then pushing a slightly new version to Heroku and the session variable key is refreshed.
Database :: Node-Redis :: This works fine locally also (also app server running locally but connecting to remote Redis instance for database), but does not seem to be working on Heroku.
WebSockets Transport Pub/Sub :: I haven't tested this thoroughly. I'm guessing it's working but it's not important for this question.
What is most interesting to me,
is that the Heroku instance is able to connect to the Redis instance via the Connect-Redis client, but is not able to connect to the same Redis instance via the database client. Whereas my locally running version is able to connect to the same (remote) instance via both of these clients. (I have verified these findings through terminal clients, using flushall command and waiting for keys to be refreshed.)
Any insight would be greatly appreciated.
I'll post my edits and updates via comments below because they're timestamped.
It turns out we can't connect to a database (not sure why because I don't know the internals of Heroku's config over AWS) the 'normal' way through Heroku; we need to set the environment variable with the heroku config:set command. OpenRedis provides a terminal ready string, can probably put something similar together for self-provisioned RedisToGo instances, though I haven't done it yet.

Resources