mongoose-express-api

Flexible automatic API generation for Moongose and Express

npm install mongoose-express-api
10 downloads in the last week
24 downloads in the last month

Mongoose Express Api

Flexible automatic API generation for Moongose and Express

Features

  • Unobtrusive mix routes into Express apps.
  • Quick setup.
  • RESTful ready to use default implementation with CRUDL (create, read, update, destroy and list) actions and JSend responses. list action supports complex queries through moongose-api-query.
  • Setup middlewares to secure api and intercept requests.
  • Choose which models to map and actions enabled to them.
  • Flexible Interface: you can change everything, modifing actions and response formats, or replace the whole implementation to use different protocols or action sets.
  • Automatic documentation generation.

Installation

npm install mongoose-express-api

or specify it as a dependency in package.json

"dependencies": {
  "mongoose-express-api": "*"
}

and then run npm install in your project root.

Examples

Basic usage

MongooseExpressApi = require("mongoose-express-api");

api = new MongooseExpressApi();
api.resources(mongoose.models);

api.mount(app, "/api");

Filtering models

api.resources(mongoose.models, { skip: ["Admin"] })

Specifying allowed actions

api.resources(mongoose.models, {actions: ["read", "list"]})

Adding a single model

api.resource(myModel)

Adding a single model filtering actions

api.resource(myModel, {actions: ["read", "list"]})

Extending implementation

impl = api.getImplementation();
impl.setResponder( myResponder );
impl.addAction( "search", searchAction );

Replacing Implementation

impl = api.setImplementation( myImpl );

Securing api and using middlewares

impl.use(myMiddleware);

impl.use(authMiddleware, {if: function(action){
  return action.method != "get";
}});

Generate Documentation

api = new MongooseExpressApi({ title: "My API", description: "This API is for ..", version: "1.0.0" });
api.resources(mongoose.models);

api.mount(app, "/api");
api.mountDocumentation(app, "/api/docs");
npm loves you