express-mongoose-db
Connect once and memorize connection for next usages
Usage
First - install middleware in project:
npm i express-mongoose-db --save
Second - add middleware to express application:
var app = ;app;
Now you can access mongoose.connection
object of mongoose
module in any following middleware or route handler in express.
API
express-mongoose-db([options])
Options
You can pass options to constructor of middleware function like this: require('express-mongoose-db')(options)
where options
is an object with fields described below.
Also you can modify defaults value in config
property of middleware contructor like this:
var mongoosedb = ;mongoosedbconfigreadPreference = 'secondary'; var app = ;app; app;
host
- server or replica string (default:localhost
, but can beserver.one.com:123,server.two.com:456
)db
- name of database (default:test
)options
- object, that passed to mongoose.connect.- And all options from connect-once, such as
reconnectWait
andheartbeat
function.
Events
To know what's up in your life, we provide event-emitter to listen to. For example - this is how you know, that reconnecton happening:
var mongoosedb = options;mongoosedbconnection;
Also you can subscribe on connection event:
var mongoosedb = options;mongoosedbconnection;
express-mongoose-db
will start attempts to connect straight after require.