简体   繁体   English

Node.js-在Heroku上使用MongoHQ连接到MongoDB

[英]Node.js - Connecting to MongoDB using MongoHQ on Heroku

I have a Node.js app using Express that stores data in a mongoDB (locally). 我有一个使用Express的Node.js应用,该应用将数据存储在mongoDB中(本地)。

It's now pushed to heroku successfully on a cedar stack and the server is running. 现在,它已成功在雪松堆栈上推送到heroku,并且服务器正在运行。 I added the mongohq addon through the terminal. 我通过终端添加了mongohq插件。

Now... how do I connect that mongoDB through mongohq to my application to start using it??? 现在...如何通过mongohq将那个mongoDB连接到我的应用程序以开始使用它??? 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: 我尝试了以下操作(使用MYPASSWORD和MYDBNUMBER的真实值:

in routes.js 在routes.js中

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

in my schema.js (also tried using the heroku 在我的schema.js中(也尝试使用heroku

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

my package.json 我的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. 现在,只有根“ /” GET成功。 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. 实际上,如果我尝试使用/ page / ANYTHING,我可以成功获取一个渲染的500错误页面,该页面可以用来处理尝试获取尚未创建的“页面”的过程……这对我来说似乎很奇怪。 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... 另外,如果我去mongohq并在那里打开数据库,则已经创建了我的模型页面的集合,并且创建了唯一性索引,即使我无法访问页面来创建或查看模型也是如此。我的应用...

happy to provide any other info... so stuck. 很乐意提供其他任何信息。

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. 我们正在将heroku用于MongoHQ,但在本例中,我们自己创建了MongoHQ帐户,因此我们的解决方案有所不同。 Now, we are using the Redis plugin and in that case we connect to Redis using the heroku env variables. 现在,我们使用Redis插件,在这种情况下,我们使用heroku env变量连接到Redis。 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: 创建新插件时,Heroku添加了一堆env变量,您可以从node.js应用轻松访问此操作:

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 如果要使用Redis,则var的名称为REDISTOGO_URL,因此我们的代码如下所示

process.env.REDISTOGO_URL

For MongoHQ the env variable seems to be MONGOHQ_URL 对于MongoHQ,env变量似乎是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. Heroku在http://devcenter.heroku.com/articles/mongohq#using_the_mongo_ruby_driver上提供了一些有关Ruby的文档,这些文档可以帮助您顺利进行。

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. 我们正在app.js上完成所有设置,这是我们在应用程序中的入口。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM