mio-express

Expose Mio resources via RESTful Express middleware.

mio-express

Expose Mio resources via Express middleware.

  • Pair with mio-ajax for automatic client-server communication.
  • PATCH support with fast-json-patch
  • Responds with 405 status for unsupported methods
  • Support Prefer header to control whether PUT and PATCH return the resource
  • Includes Location header for created resources
  • Emits events for accessing requests
  • Query parameter type coercion

Example

var mio = require('mio');
var ServerRoutes = require('mio-express').plugin;
 
var User = mio.Resource.extend({
  attributes: {
    id: { primary: true }
  }
}, {
  baseUrl: '/users'
});
 
User.use(ServerRoutes());

This will expose Express routing middleware via User.router:

var bodyParser = require('body-parser');
var express = require('express');
var app = express();
 
app
  .use(bodyParser.json())
  .use(User.router);
var Item = Resource.extend({
  attributes: {
    stock: {
      coerce: 'number'
    }
  }
})

Install using npm:

npm install mio-express

Members

##mio-express.plugin([options]) Returns Mio plugin function.

Handlers return http-errors. These should be handled by your downstream error handling middleware.

In order to convert the intended query parameters into numbers, add coerce: "number" to the resource's attribute defintion.

Params

  • [options] Object

Returns: MioExpressPlugin
Example

User.use(require('mio-express').plugin());

Type coercion:

var Item = Resource.extend({
  attributes: {
    stock: {
      coerce: 'number'
    }
  }
})

##event: "request" Emitted by route handlers on request.

Params

  • request express.Request
  • next function

Scope: inner event of mio-express
Example

Resource.hook('request', function (reqnext) {
  req.body.createdBy = req.session.userId;
  next();
});

##event: "response" Emitted by route handlers on response.

Params

  • response express.Response
  • next function

Scope: inner event of mio-express
Example

Resource.hook('response', function (resnext) {
  res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: true });
  next();
});

##event: "request:get" Emitted by route handlers on GET request.

Params

  • request express.Request
  • next function

Scope: inner event of mio-express
##event: "response:get" Emitted by route handlers on GET response.

Params

  • response express.Response
  • resource mio.Resource
  • next function

Scope: inner event of mio-express
##event: "request:post" Emitted by route handlers on POST request.

Params

  • request express.Request
  • next function

Scope: inner event of mio-express
##event: "response:post" Emitted by route handlers on POST response.

Params

  • response express.Response
  • result mio.Resource | mio.Resource.Collection
  • next function

Scope: inner event of mio-express
##event: "request:put" Emitted by route handlers on PUT request.

Params

  • request express.Request
  • next function

Scope: inner event of mio-express
##event: "response:put" Emitted by route handlers on PUT response.

Params

  • response express.Response
  • resource mio.Resource
  • next function

Scope: inner event of mio-express
##event: "request:patch" Emitted by route handlers on PATCH request.

Params

  • request express.Request
  • next function

Scope: inner event of mio-express
##event: "response:patch" Emitted by route handlers on PATCH response.

Params

  • response express.Response
  • resource mio.Resource
  • next function

Scope: inner event of mio-express
##event: "request:delete" Emitted by route handlers on DELETE request.

Params

  • request express.Request
  • next function

Scope: inner event of mio-express
##event: "response:delete" Emitted by route handlers on DELETE response.

Params

  • response express.Response
  • resource mio.Resource
  • next function

Scope: inner event of mio-express
##event: "request:collection:get" Emitted by route handlers on collection GET request.

Params

  • request express.Request
  • next function

Scope: inner event of mio-express
##event: "response:collection:get" Emitted by route handlers on collection GET response.

Params

  • response express.Response
  • collection mio.Resource.Collection
  • next function

Scope: inner event of mio-express
##event: "request:collection:patch" Emitted by route handlers on collection PATCH request.

Params

  • request express.Request
  • next function

Scope: inner event of mio-express
##event: "response:collection:patch" Emitted by route handlers on collection PATCH response.

Params

  • response express.Response
  • collection mio.Resource.Collection
  • next function

Scope: inner event of mio-express
##event: "request:collection:delete" Emitted by route handlers on collection DELETE request.

Params

  • request express.Request
  • next function

Scope: inner event of mio-express
##event: "response:collection:delete" Emitted by route handlers on collection DELETE response.

Params

  • response express.Response
  • next function

Scope: inner event of mio-express

Please submit all issues and pull requests to the mio/mio-express repository!

Run tests using npm test or gulp test.

Generate code coverage using gulp coverage and open coverage.html in your web browser.

If you have any problem or suggestion please open an issue here.