http-server-request-handlers-db
v0.1.5
Published
an http.Server request handler that attempts to re-initialize a db connection with app.setupDb
Downloads
7
Maintainers
Readme
http-server-request-handlers-db
an http.Server request handler that attempts to re-initialize a db connection with app.setupDb
; it should be used in conjunction with the mongoose-helpers-setup-db module.
the initial intent of this module is to provide a way to re-initialize a mongoose connection, on an http request, if the connection threw an error on application startup, or was lost after startup.
table of contents
notes
- expects
req.app
to exist - interacts with the following properties on the
app
if they existapp.debug
- when set to true, the request handler willconsole
log an info statementapp.db.error
- when not set, the request handler will not attempt to re-initialize the db connectionapp.setupDb
- a promise that that will return thedb
connection or reject with anError
- only called when
app.db.error
is set andapp.setupDb
is a function - will set
app.db
to the returned db connection - if the promise is rejected with an error, the request handler will:
console.error
the error returned- alter the error to a user error if
NODE_ENV
is not set todevelopment
- set the error
statusCode
to500
- set the
app.db.error
to the error - return
next( app.db.error )
- only called when
installation
npm install http-server-request-handlers-db
api
/**
* @param {IncomingMessage} req
* @param {Object} req.app
*
* @param {ServerResponse} res
* @param {Function} next
*
* @returns {undefined}
*/
function dbRequestHandler( req, res, next )
usage
basic
var dbRequestHandler = require( 'http-server-request-handlers-db' )
function route( router ) {
router.get( '/', [
dbRequestHandler,
require( '../controllers/home/get' )
]
)
}