mio-express
v0.13.4
Published
Expose Mio resources via RESTful Express middleware.
Downloads
7
Readme
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);
Query parameter type coercion:
var Item = Resource.extend({
attributes: {
stock: {
coerce: 'number'
}
}
})
Installation
Install using npm:
npm install mio-express
API Reference
Members
- mio-express
- mio-express.plugin([options])
- event: "request"
- event: "response"
- event: "request:get"
- event: "response:get"
- event: "request:post"
- event: "response:post"
- event: "request:put"
- event: "response:put"
- event: "request:patch"
- event: "response:patch"
- event: "request:delete"
- event: "response:delete"
- event: "request:collection:get"
- event: "response:collection:get"
- event: "request:collection:patch"
- event: "response:collection:patch"
- event: "request:collection:delete"
- event: "response:collection:delete"
- mio-express~mio
##mio-express.plugin([options]) Returns Mio plugin function.
Handlers return http-errors
.
These should be handled by your downstream error handling middleware.
Type coercion
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 (req, next) {
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 (res, next) {
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
Contributing
Please submit all issues and pull requests to the mio/mio-express repository!
Tests
Run tests using npm test
or gulp test
.
Code coverage
Generate code coverage using gulp coverage
and open coverage.html
in your
web browser.
Support
If you have any problem or suggestion please open an issue here.