我正在尝试做这个人在这里做同样的事情How do I setup MongoDB database on Heroku with MongoLab?
该应用程序适用于Amazon EC2,我正在使用MongoLabs插件部署到Heroku。
我应该输入什么来更改与Mongo URI的mongo连接?
/** https://devcenter.heroku.com/articles/getting-started-with-nodejs#write-your-app */
var mongo = require('mongodb');
var mongoUri = process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/mydb';
mongo.Db.connect(mongoUri, function (err, db) {
db.collection('mydocs', function(er, collection) {
collection.insert({'mykey': 'myvalue'}, {safe: true}, function(er,rs) {
});
});
});
/** app.js */
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, EmployeeProvider = require('./employeeprovider').EmployeeProvider;
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 8080);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('view options', {layout: false});
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
var employeeProvider= new EmployeeProvider('localhost', 27017);
//Routes
app.get('/', function(req, res){
employeeProvider.findAll(function(error, emps){
res.render('index', {
title: 'Employees',
employees:emps
});
});
});
app.get('/employee/new', function(req, res) {
res.render('employee_new', {
title: 'New Employee'
});
});
//save new employee
app.post('/employee/new', function(req, res){
employeeProvider.save({
title: req.param('title'),
name: req.param('name')
}, function( error, docs) {
res.redirect('/')
});
});
app.listen(8080);
var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
var BSON = require('mongodb').BSON;
var ObjectID = require('mongodb').ObjectID;
EmployeeProvider = function(host, port) {
this.db= new Db('node-mongo-employee', new Server(host, port, {safe: true}, {auto_reconnect: true}, {}));
this.db.open(function(){});
};
EmployeeProvider.prototype.getCollection= function(callback) {
this.db.collection('employees', function(error, employee_collection) {
if( error ) callback(error);
else callback(null, employee_collection);
});
};
//find all employees
EmployeeProvider.prototype.findAll = function(callback) {
this.getCollection(function(error, employee_collection) {
if( error ) callback(error)
else {
employee_collection.find().toArray(function(error, results) {
if( error ) callback(error)
else callback(null, results)
});
}
});
};
//save new employee
EmployeeProvider.prototype.save = function(employees, callback) {
this.getCollection(function(error, employee_collection) {
if( error ) callback(error)
else {
if( typeof(employees.length)=="undefined")
employees = [employees];
for( var i =0;i< employees.length;i++ ) {
employee = employees[i];
employee.created_at = new Date();
}
employee_collection.insert(employees, function() {
callback(null, employees);
});
}
});
};
exports.EmployeeProvider = EmployeeProvider;
我如何更改employeeProvider以使用URI而不是localhost?
答案 0 :(得分:10)
var mongoUri = process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/mydb';
此行应该已经正确,无需更改任何内容。只要其中一个值存在,代码就会短路,因此只有在环境变量中不存在MONGOLAB_URI或MONGOHQ_URL时才使用'mongodb:// localhost / mydb'。
也就是说,一个给定的Heroku应用程序可能没有使用两个MongoDB提供程序,因此只包含您打算使用的变量名称是有意义的。例如,我的应用有:
var mongoUri = process.env.MONGOLAB_URI || 'mongodb://localhost/test'
答案 1 :(得分:1)
首先检查 NODE_ENV 环境
$ heroku config
=== ### Config Vars
MONGODB_URI: mongodb://heroku_lhwpq2p3:sdvsdvsdvds@sdvsdv.mlab.com:dsvsd/heroku_lsdv
NODE_ENV: production
NPM_CONFIG_PRODUCTION: true
你在var
中输入的名字mongoUri = process.env.MONGOLAB_URI || localhost:..
应该是:
var mongoUri = process.env.MONGODB_URI || localhost:..
如果错误仍然存在,那么您需要将 NODE_ENV 环境变量从开发设置为生产,如:
$ heroku config:set NODE_ENV="production"
$ heroku config:set NPM_CONFIG_PRODUCTION=true
答案 2 :(得分:0)
我使用的是MongoHQ但它的类似,首先你需要在数据库的插件中创建用户,然后将凭据添加到URI(你应该在你的mongo插件中看到uri) 例如:
的mongodb://用户:pass@paulo.mongohq.com:10000 / app12345678