http-server-request-handlers-db

0.1.5 • Public • Published

http-server-request-handlers-db

NPM version Build Status NSP Status

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 exist
    • app.debug - when set to true, the request handler will console log an info statement
    • app.db.error - when not set, the request handler will not attempt to re-initialize the db connection
    • app.setupDb - a promise that that will return the db connection or reject with an Error
      • only called when app.db.error is set and app.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 to development
        • set the error statusCode to 500
        • set the app.db.error to the error
        • return next( app.db.error )

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' )
    ]
  )
}

license

MIT License

Package Sidebar

Install

npm i http-server-request-handlers-db

Weekly Downloads

4

Version

0.1.5

License

MIT

Unpacked Size

22.7 kB

Total Files

14

Last publish

Collaborators

  • dentous