UNPKG

mongo-web-handler

Version:

This a helper for creating ExpressJS Web Handler for MongoDB querying.

44 lines (26 loc) 1.18 kB
#mongo-web-handler ## Dependencies `Q` : for promise management `Log4js` : for login API `MongoJS` : API for MongoDB ## Description This is a helper function for ExpressJS handler creation. The aim is factorization of code : querying - waiting for results - writing JSon response for a HTTP Rest Server. ## Installation `npm install mongo-web-handler --save` ## Usage Usage example : ``` var MONGOWH = require('mongo-web-handler'); app.route("/items") .get( MONGOWH.createMongoWebHandler( logger, "ItemListQuery" function( req, res, resultsCallbackFunc ){ mongoItemsCollection.find( {}, resultsCallbackFunc ); }) ); ``` The function _createMongoWebHandler_ returns an ExpressJS handler function. It takes parameters : * a **logger** : a log4js logger for tracing ; * a **query name** : for log output * a **query function** for your querying logic with one argument, the result callback function so the handler can parse the results end return a JSon response. See _server.js_ and run it for real usage sample ; type `nom start`. Is assumes that you have a Local MongoDB running with a `MongoWebHandlerDB` database and a collection named `items`.