Setting in an environment variable MONGOHQ_URL
What i'm doing these days is to try setting up a web application using Heroku as a solution with mongoDB, Node.js. Using Heroku's free features and limited mongoHQ free storage, you may make a light web app. To provide a fully functional website which communicates with server and DB, the first job for you is to think of server side code.
A small part of it, I faced a critical obstacle on 'How to set up a connection with mongoHQ Add-on, Heroku.' It took me around 90minutes or so. I'd been googling and got some answers from 'Stack Overflow', 'Groups of Google' or so. But the answer is just simple as if you know node.js global object named 'process'.
http://nodejs.org/api/process.html#process_processI don't fully understand what it is and how it works, but I'm using it to catch unexpected errors that may shut down its server, and to set a variable for mongoHQ URL :)
You may easily find the url -http://support.mongohq.com/languages/nodejs.html while googling, and you may understand the whole explanation, going down the scroll. However!! The problem we have here is where to put the following line-
var MONGOHQ_URL = "mongodb://<user>:<password>@linus.mongohq.com:<port>/<db name>";
Even if you don't know anything on 'process', you may know what javascript object is and how to use it. If so, we simply override the object like this to make it works.var mongodb = require('mongodb');var url = require('url');var log = console.log;var connectionUri = url.parse(process.env.MONGOHQ_URL);var dbName = connectionUri.pathname.replace(/^\//, '');mongodb.Db.connect(process.env.MONGOHQ_URL, function(error, client) {if (error) throw error;....
If you got here while looking up for a proper answer to set in an environment variable, you might understand what it means. However, since we simply override the 'process' object, it may causes problems or errors somewhere to access the object if you already use it in different areas. Further researching and leaning relative information is required.process.env={ MONGOHQ_URL:"mongodb://<user>:<password>@linus.mongohq.com:<port>/<db name>" };
Good day :)
댓글
댓글 쓰기