Node.js-在Heroku上使用MongoHQ连接到MongoDB [英] Node.js - Connecting to MongoDB using MongoHQ on Heroku
问题描述
我有一个使用Express的Node.js应用程序,该应用程序将数据存储在mongoDB(本地)中.
I have a Node.js app using Express that stores data in a mongoDB (locally).
现在它已成功在雪松堆栈上推送到heroku,并且服务器正在运行.我通过终端添加了mongohq插件.
It's now pushed to heroku successfully on a cedar stack and the server is running. I added the mongohq addon through the terminal.
现在...如何通过mongohq将那个mongoDB连接到我的应用程序以开始使用它???我在网上任何地方都看不到有关此内容的教程.如果您可以将我引向一个人或让我开始在哪里添加配置,我将不胜感激!
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!
非常感谢.
更新:
我尝试了以下操作(使用MYPASSWORD和MYDBNUMBER的真实值:
I've tried the following (with real values for MYPASSWORD and MYDBNUMBER:
在routes.js中
in routes.js
var db = mongoose.connect('mongodb://heroku:<MYPASSWORD>@staff.mongohq.com:10049/<MYDBNUMBER>');
在我的schema.js中(也尝试使用heroku
in my schema.js (also tried using the heroku
var mongoose = require('mongoose');
mongoose.connect('mongodb://heroku:<MYPASSWORD>@staff.mongohq.com:10049/<MYDBNUMBER>');
我的package.json
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"
}
}
现在,只有根'/'GET成功.实际上,如果我尝试使用/page/ANYTHING,我可以成功获取一个渲染的500错误页面,该页面可以用来处理试图获取尚未创建的页面"的尝试.其他一切都给我一个内部服务器错误.
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.
此外,如果我去mongohq并在那打开数据库,则已经创建了模型页面的集合,并且创建了唯一性索引,即使我无法访问页面也无法创建或查看我的应用程序中的模型...
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.
推荐答案
我们在针对MongoHQ使用heroku,但在本例中,我们是自己创建了MongoHQ帐户,因此我们的解决方案有所不同. 现在,我们使用Redis插件,在这种情况下,我们使用heroku env变量连接到Redis. 创建新插件时,Heroku添加了一堆env变量,您可以从node.js应用轻松访问此变量:
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
如果要使用Redis,则var的名称为REDISTOGO_URL,因此我们的代码如下所示:
In the case of Redis to go the name of the var is REDISTOGO_URL so our code looks like
process.env.REDISTOGO_URL
对于MongoHQ,env变量似乎是MONGOHQ_URL
For MongoHQ the env variable seems to be MONGOHQ_URL
您可能需要进行一些解析. Heroku在此处 http://devcenter.heroku.com/articles/mongohq#using_the_mongo_ruby_driver 应该可以帮助您实现目标.
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.
关于在快速应用程序中的何处进行操作.我们正在app.js上完成所有设置,这是我们在应用程序中的入口点.
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.
希望这会有所帮助.
这篇关于Node.js-在Heroku上使用MongoHQ连接到MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!