我正在尝试做这个人在这里做的同样的事情如何使用 MongoLab 在 Heroku 上设置 MongoDB 数据库?
该应用程序在 Amazon EC2 上运行,我正在使用 MongoLabs 插件部署到 Heroku。
我究竟应该键入什么来更改与 Mongo URI 的 mongo 连接?
Heroku 文档
/** 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) {
});
});
});
应用程序.js
/** 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);
员工提供者.js
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 而不是本地主机?