Express middleware for MongoDB REST APIs
(Object
= {}
)
options for this function.
Name | Description |
---|---|
options.express Object
(default {} )
|
options for express JavaScript package |
options.express.database string
(default 'database' )
|
route parameter
name in path (e.g.
/:database
) for MongoDB database
|
options.express.collection string
(default 'collection' )
|
route parameter
name in path (e.g.
/:collection
) for MongoDB collection
|
options.express.method string
(default 'method' )
|
route parameter
name in path (e.g.
/:method
) for MongoDB method
|
options.express.parse function
|
function for parsing the
query string
into an appropriate format to be passed to each
options.mongodb.handler
or
options.rest.<METHOD>.handler
|
options.express.handler function
|
function for handling responses after connecting to MongoDB
|
options.express.allow Object
(default {} )
|
options for allowing access to databases and collections |
options.express.allow.database Array
(default [] )
|
names of MongoDB databases to allow API access to
|
options.express.allow.collection Array
(default [] )
|
names of MongoDB collections to allow API access to
|
options.express.allow.method Array
(default [] )
|
names of MongoDB methods to allow API access to
|
options.express.allow.code number
(default 400 )
|
response status code when a request is not allowed |
options.express.deny Object
(default {} )
|
options for denying access to databases and collections
|
options.express.deny.database Array
(default ['admin'] )
|
names of MongoDB databases to deny API access to
|
options.express.deny.collection Array
(default [] )
|
names of MongoDB collections to deny API access to
|
options.express.deny.method Array
(default [] )
|
names of MongoDB methods to deny API access to
|
options.express.deny.code number
(default 400 )
|
response status code when a request is denied |
options.mongodb Object
(default {} )
|
default options for MongoDB database. |
options.mongodb.connection string
(default process.env.MONGODB_CONNECTION||'mongodb://localhost:27017' )
|
MongoDB connection string . |
options.mongodb.options (Object | string)
(default process.env.MONGODB_OPTIONS )
|
Mongodb connect options . |
options.mongodb.database string
(default process.env.MONGODB_DATABASE||'test' )
|
database name.
|
options.mongodb.collection string
(default process.env.MONGODB_COLLECTION||'express_mongodb_rest' )
|
collection name
|
options.rest Object
(default {} )
|
options for REST API definitions
|
options.rest.GET Object
(default {} )
|
example of REST API definition for
GET
|
options.rest.GET.database string
(default options.mongodb.database )
|
default database name for
GET
request
|
options.rest.GET.collection string
(default options.mongodb.collection )
|
default collection name for
GET
request
|
options.rest.GET.method string
(default 'find' )
|
default
collection method
name for
GET
request
|
options.rest.GET.query Array
|
base query when URL query is not provided for
collection method
defined by
options.rest.GET.method
|
options.rest.GET.handler Object
(default {} )
|
object containing handler functions as defined in
options.express.handler
|
options.rest.GET.handler.find function
(default options.express.handler )
|
function to handle responses for a method after connecting to mongodb as defined in
options.express.handler
|
function
:
Express
middleware
compatible with
app.use
.
var express = require('express');
var api = require('express-mongodb-rest');
// (options) Initialize options object
var options = {mongodb: {}, rest: {}};
// (connection_mongodb) Setup mongodb connection
// Format: 'mongodb://<user>:<password>@<host>:<port>'
options.mongodb.connection = 'mongodb://localhost:27017'; // process.env.MONGODB_CONNECTION
options.mongodb.options = {poolSize: 10};
options.mongodb.database = 'test'; // process.env.MONGODB_DATABASE
options.mongodb.collection = 'express_mongodb_rest'; // process.env.MONGODB_COLLECTION
// (options_get) GET options
options.rest.GET = {};
options.rest.GET.method = 'find';
options.rest.GET.query = {q: {}}; // return all if no query string provided
// (options_get_limit) Limit number of documents for GET to 100
options.rest.GET.handler = {};
options.rest.GET.handler.find = function(req, res, next, data) {
var collection = data.mongodb.collection;
var query = data.rest.query;
collection.find(query.q, query.options, function(err, result) {
if (err) next(err);
result.limit(100).toArray(function(err, docs) {
if (err) next(err);
res.json(docs);
});
});
};
// (options_get_count) Handle count collection method
options.rest.GET.handler = {};
options.rest.GET.handler.count = function(req, res, next, data) {
var collection = data.mongodb.collection;
var query = data.rest.query;
collection.count(query.q, query.options, function(err, result) {
if (err) next(err);
res.json({count: result};
});
};
// (options_post) POST options
options.rest.POST = {};
options.rest.POST.method = 'insertMany';
// (options_post) POST options
options.rest.PUT = {};
options.rest.PUT.method = 'updateMany';
// (options_delete) DELETE options
options.rest.DELETE = {};
options.rest.DELETE.method = 'deleteMany';
// (app) Create express app
var app = express();
// (app_middleware) Add MongoDB REST API on localhost:3000/api
app.use('/api', api(options);
app.use('/api/:collection', api(options)); // enable other collections
app.use('/api/:database/:collection', api(options)); // enable other databases and collections
app.use('/api/:database/:collection/:method', api(options)); // enable other databases, collections, and methods
// (app_start) Listen on localhost:3000
app.listen(3000);