使用cloud9上的node.js应用程序连接到MongoHQ

时间:2014-01-08 20:09:11

标签: javascript node.js mongoose cloud9-ide mongohq

我需要一些帮助将我的node.js应用程序(我在cloud9中编辑)连接到MongoHQ的数据库。到目前为止,我已通过终端成功连接到数据库,并能够执行一些命令:

use drywall;
db.admingroups.insert({ _id: 'root', name: 'Root' });
db.admins.insert({ name: {first: 'Root', last: 'Admin', full: 'Root Admin'}, groups: ['root'] });
var rootAdmin = db.admins.findOne();
db.users.save({ username: 'root', isActive: 'yes', email: 'your@email.addy', roles: {admin: rootAdmin._id} });
var rootUser = db.users.findOne();
rootAdmin.user = { id: rootUser._id, name: rootUser.username };
db.admins.save(rootAdmin);

但我不知道如何将我的应用程序连接到数据库,所以当我尝试运行app.js(我知道它不会起作用)时,我收到此错误:

mongoose connection error:  [Error: failed to connect to [localhost:27017]]
/var/lib/stickshift/52c97a42e0b8cde840000069/app-root/data/727946/drywall/node_modules/connect-mongo/lib/connect-mongo.js:176
      throw new Error('Error connecting to database');
^
Error: Error connecting to database
at /var/lib/stickshift/52c97a42e0b8cde840000069/app-root/data/727946/drywall/node_modules/connect-mongo/lib/connect-mongo.js:176:17
at /var/lib/stickshift/52c97a42e0b8cde840000069/app-root/data/727946/drywall/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:273:18
at /var/lib/stickshift/52c97a42e0b8cde840000069/app-root/data/727946/drywall/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:351:18
at Server.close (/var/lib/stickshift/52c97a42e0b8cde840000069/app-root/data/727946/drywall/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/server.js:210:38)
at Db.close (/var/lib/stickshift/52c97a42e0b8cde840000069/app-root/data/727946/drywall/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:347:21)
at /var/lib/stickshift/52c97a42e0b8cde840000069/app-root/data/727946/drywall/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:271:21
at null. (/var/lib/stickshift/52c97a42e0b8cde840000069/app-root/data/727946/drywall/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/server.js:563:7)
at EventEmitter.emit (events.js:106:17)
at null. (/var/lib/stickshift/52c97a42e0b8cde840000069/app-root/data/727946/drywall/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15)
at EventEmitter.emit (events.js:98:17)

(在我看来我试图连接到我的localhost:27017,但我真的不知道我应该连接到什么)

这是app.js:

'use strict';

//dependencies
var config = require('./config'),
    express = require('express'),
    mongoStore = require('connect-mongo')(express),
    http = require('http'),
    path = require('path'),
    passport = require('passport'),
    mongoose = require('mongoose');

//create express app
var app = express();

//setup the web server
app.server = http.createServer(app);

//setup mongoose
app.db = mongoose.createConnection(config.mongodb.uri);
app.db.on('error', console.error.bind(console, 'mongoose connection error: '));
app.db.once('open', function () {
  //and... we have a data store
});

//config data models
require('./models')(app, mongoose);

//setup the session store
app.sessionStore = new mongoStore({ url: config.mongodb.uri });

//config express in all environments
app.configure(function(){
  //settings
  app.disable('x-powered-by');
  app.set('port', config.port);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.set('strict routing', true);
  app.set('project-name', config.projectName);
  app.set('company-name', config.companyName);
  app.set('system-email', config.systemEmail);
  app.set('crypto-key', config.cryptoKey);
  app.set('require-account-verification', config.requireAccountVerification);

  //smtp settings
  app.set('smtp-from-name', config.smtp.from.name);
  app.set('smtp-from-address', config.smtp.from.address);
  app.set('smtp-credentials', config.smtp.credentials);

  //twitter settings
  app.set('twitter-oauth-key', config.oauth.twitter.key);
  app.set('twitter-oauth-secret', config.oauth.twitter.secret);

  //github settings
  app.set('github-oauth-key', config.oauth.github.key);
  app.set('github-oauth-secret', config.oauth.github.secret);

  //facebook settings
  app.set('facebook-oauth-key', config.oauth.facebook.key);
  app.set('facebook-oauth-secret', config.oauth.facebook.secret);

  //middleware
  app.use(express.favicon(__dirname + '/public/favicon.ico'));
  app.use(express.logger('dev'));
  app.use(express.static(path.join(__dirname, 'public')));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({
    secret: config.cryptoKey,
    store: app.sessionStore
  }));
  app.use(passport.initialize());
  app.use(passport.session());
  app.use(app.router);

  //error handler
  app.use(require('./views/http/index').http500);

  //global locals
  app.locals.projectName = app.get('project-name');
  app.locals.copyrightYear = new Date().getFullYear();
  app.locals.copyrightName = app.get('company-name');
  app.locals.cacheBreaker = 'br34k-01';
});

//config express in dev environment
app.configure('development', function(){
  app.use(express.errorHandler());
});

//setup passport
require('./passport')(app, passport);

//route requests
require('./routes')(app, passport);

//setup utilities
app.utility = {};
app.utility.sendmail = require('drywall-sendmail');
app.utility.slugify = require('drywall-slugify');
app.utility.workflow = require('drywall-workflow');

//listen up
app.server.listen(app.get('port'), function(){
  //and... we're live
});

我尝试添加

var db = mongoose.connect('mongodb://<user>:<password>@alex.mongohq.com:10067/drywall');

到app.js但没有用。它仍然想要localhost。

2 个答案:

答案 0 :(得分:1)

对于遇到此问题的其他任何人,解决方案就在这里:https://docs.c9.io/setting_up_mongodb.html

MongoDB预安装在Cloud9工作区中。运行这个:

$ mkdir data
$ echo 'mongod --bind_ip=$IP --dbpath=data --nojournal --rest "$@"' > mongod
$ chmod a+x mongod

要启动Mongodb进程,请运行:

$ ./mongod

然后&#39;运行&#39;你的node.js应用程序脚本和你参加比赛。

以下是参数的含义:

  

- dbpath = data(因为它默认为/ var / db,不可访问)

     

- nojournal因为mongodb通常预先分配2 GB日志文件(超过Cloud9磁盘空间配额)

     

- bind_ip = $ IP(因为你不能绑定到0.0.0.0)

     

- 休息在默认端口28017上运行

答案 1 :(得分:0)

mongodb.uri在config.js中定义:

exports.mongodb = {
  uri: process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || 'localhost/drywall'
};

如果没有设置任何过程变量,则默认为localhost。 尝试在上面的代码之前将process.env.MONGOHQ_URL设置为mongodb://<user>:<password>@alex.mongohq.com:10067/drywall