I've got a fairly "typical" setup (I'm using RailwayJS)
schema:
customSchema(function(){
// Initialize Mongo DB
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/mydb'); //todo: needs to come from connection or something?!
var MyModel = new Schema({
name : String,
email : String,
something : String
});
});
My question is, how can I specify the connection string to use?
Should I move the mongoose.connect part out into my config section?
This is what I like to use :
var mongourl = process.argv[2] || process.env.NODE_DB || 'mongodb://anywhere/db';
Remember to change NODE_DB
to whatev er your env variable name is.
Note that I also allow to pass command line args (useful when using Cloud9 for running applications as this awesome service does not allow env variable). Index is 2 because the command is node app.js mongodb://address
, remember to adjust if needed.