API Configuration

Router configuration

To expose mongoose.Model you should create an API Router. It could be done in two ways:

  • By calling ModelAPIExpress.expose method
  • By creating ModelAPIRouter and then attaching it with method attachTo to ModelAPIExpress

Using ModelAPIExpress.expose:

const api = new merest.ModelAPIExpress();

api.expose(models.Vector, {
  // router configuration parameters should be here
});

Creating ModelAPIRouter explicitly:

const api = new merest.ModelAPIExpress();
const vectorApi = new merest.ModelAPIRouter(models.Vector, {
  // router configuration parameters should be here
});

vectorApi.attachTo(api);

merest supports wide range of router configuration parameters:

var api = new merest.ModelAPIExpress();
api.expose(models.Vector, {
  path: '/cool-vectors', // overrides standard /model-plural-name path
  options: false, // disables end-point for OPTIONS method
  create: 'get', // mounts the CREATE end-point on the HTTP GET method
  search: 'post', // mounts the SEARCH end-point on the HTTP POST to resolve path-conflict with CREATE end-point
  details: 'Show details for a vector', // assigns the description for DETAILS end-point
  update: 'put', // mounts UPDATE end-point to the HTTP PUT
  delete: false, // disables to delete vectors
  fields: 'x y', // forces to response with x, y and _id fields
  matchId: '\d+' // configures the /:id routes with integer (not uuid) :id parameter
});

All router configuration parameters are:

  • path: String - the base path for all router end-points. It is relative to the api mounted path
  • middlewares: Function|Array(Function) - the middleware(s) that should be mounted on the base path of all end-points
  • matchId: String|RegExp - the pattern to match values of id (_id) field in the end-point path. The default is '[a-f\\d]{24}'
  • options: Boolean|String|Object - configuration for OPTIONS HTTP-method
  • create: Boolean|String|Object - configuration for Instance creation
  • search: Boolean|String|Object - configuration for searching of instances-
  • details: Boolean|String|Object - configuration for instance details end-point
  • update: Boolean|String|Object - configuration for instance update
  • delete: Boolean|String|Object - configuration for instance removing
  • expose: Object - configuration for instance method(s) exposition. (see. Model methods for details)
  • exposeStatic: Object - configuration for static method(s) exposition (see. Model methods for details)

Additionally some of end-point parameters could be specified on the router configuration level: fields, populate, filter, readonly

Also some of end-points parameters are applicable exactly for one end-point SEARCH. So it is reasonable to define them in the router level. These parameters are: queryFields, sort, limit, skip,

Parameters named as end-points (create, search, etc.) could be one of: Boolean, String and Object.

In case of Boolean:
  • false: appropriate end-point is disabled
  • true: the end-point is allowed and default or common (see further) options will be used to configure it
In case of String:

If value is some of HTTP-method supported by Express (checkout, copy, delete, get, head, lock, merge, mkactivity, mkcol, move, 'm-search', notify, options, patch, post, purge, put, report, search, subscribe, trace, unlock, unsubscribe), then the appropriate method is allowed and will be mounted on specified HTTP-method. Otherwise the value will be used as a description of the appropriate end-point returned by the OPTIONS HTTP-method and swagger api-documentation

In case of Object:

The appropriate end-point is allowed and keys of the value will be used to configure this end-point.

The end-point configuration options are bellow.



Next (Installation) >