mongo-web-handler
v1.0.3
Published
This a helper for creating ExpressJS Web Handler for MongoDB querying.
Downloads
3
Maintainers
Readme
#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
.