npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mio-express

v0.13.4

Published

Expose Mio resources via RESTful Express middleware.

Downloads

7

Readme

mio-express Build Status Coverage Status NPM version Dependency Status

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.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.