• Stars
    star
    147
  • Rank 251,347 (Top 5 %)
  • Language
    JavaScript
  • Created almost 11 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

RESTful rails-style resource routing for koa

koa-resource-router

Build Status Dependency Status NPM version

RESTful resource routing for koa.

  • Rails-like REST resource routing.
  • Use multiple middleware for resource actions.
  • Responds to OPTIONS requests with allowed methods.
  • Returns 405 Method Not Allowed when applicable.

Installation

Install using npm:

npm install koa-resource-router

API

new Resource(path, actions, options)

var Resource = require('koa-resource-router');
var app = require('koa')();

var users = new Resource('users', {
  // GET /users
  index: function *(next) {
  },
  // GET /users/new
  new: function *(next) {
  },
  // POST /users
  create: function *(next) {
  },
  // GET /users/:id
  show: function *(next) {
  },
  // GET /users/:id/edit
  edit: function *(next) {
  },
  // PUT /users/:id
  update: function *(next) {
  },
  // DELETE /users/:id
  destroy: function *(next) {
  }
});

app.use(users.middleware());

Action mapping

Actions are then mapped accordingly:

GET     /users             ->  index
GET     /users/new         ->  new
POST    /users             ->  create
GET     /users/:user       ->  show
GET     /users/:user/edit  ->  edit
PUT     /users/:user       ->  update
DELETE  /users/:user       ->  destroy

Overriding action mapping

var users = new Resource('users', actions, {
  methods: {
    update: 'PATCH'
  }
});

Top-level resource

Omit the resource name to specify a top-level resource:

var root = new Resource(require('./frontpage'));

Top-level controller actions are mapped as follows:

GET     /          ->  index
GET     /new       ->  new
POST    /          ->  create
GET     /:id       ->  show
GET     /:id/edit  ->  edit
PUT     /:id       ->  update
DELETE  /:id       ->  destroy

Nesting

Resources can be nested using resource.add():

var forums = new Resource('forums', require('./forum'));
var threads = new Resource('threads', require('./threads'));

forums.add(threads);

Multiple middleware

Run middleware before resource actions by passing middleware functions before your actions:

var users = new Resource('users', authorize, actions);

Run middleware for specific actions by passing an array:

var users = new Resource('users', {
  show: [authorize, function *(next) {
    // ...
  }]
});

MIT Licensed

More Repositories

1

purescript-pux

Build type-safe web apps with PureScript.
PureScript
566
star
2

sticky

Simple, key/value pair browser-storage cache leveraging the latest HTML5 storage APIs.
JavaScript
324
star
3

jsx-transform

JSX transpiler. A standard and configurable implementation of JSX decoupled from React.
JavaScript
293
star
4

pux-starter-app

Starter Pux app w/ hot-reloading and isomorphic routing and rendering
PureScript
102
star
5

twain

Tiny web application framework for WAI.
Haskell
66
star
6

pux-devtool

Pux time-travelling devtool.
JavaScript
43
star
7

gulp-file

Create vinyl files from a string or buffer and insert into the Gulp pipeline.
JavaScript
42
star
8

mongoose-populate-virtuals

Extend Mongoose 4+ population with virtual attributes that can be populated in either direction.
JavaScript
33
star
9

virtual-dom-component

A virtual component (view). Virtual components expose events, state lens, and a render function.
JavaScript
30
star
10

virtual-dom-stringify

Deprecated. Use https://github.com/nthtran/vdom-to-html/.
JavaScript
25
star
11

html-virtualize

Parse HTML into virtual-dom tree.
JavaScript
20
star
12

gulp-jsx

virtual-dom-jsx for gulp
JavaScript
14
star
13

pux-todomvc

Pux TodoMVC
PureScript
13
star
14

express-elasticsearch-logger

Log requests to ElasticSearch.
JavaScript
12
star
15

modella-resource

Expose Modella models via RESTful resource middleware.
JavaScript
10
star
16

watch

Watch files and folders for changes, and run commands when they change. Linux, OS X, and Windows are supported.
Go
8
star
17

hyperobject

A simple object model for working with Linked Data.
JavaScript
7
star
18

dotfiles

My OS X setup using Nu shell and Helix editor.
Nushell
7
star
19

pux-css

Render purescript-css to a Pux attribute.
PureScript
5
star
20

modella-mysql

MySQL plugin for Modella.
JavaScript
4
star
21

mongoose-express-router

Create Express 4 router and middleware from Mongoose 4 model.
JavaScript
4
star
22

charisi

A rich text editor for the web, built for speed and stability
JavaScript
3
star
23

purescript-markdown-smolder

Render purescript-markdown to purescript-smolder.
PureScript
3
star
24

webmention

Types and functions for working with webmentions.
Haskell
3
star
25

pux-websockets

PureScript
3
star
26

wai-responder

A tiny web application framework for WAI.
Haskell
2
star
27

modella-memory

Memory persistence layer for Modella. Useful for development or as a reference implementation for Modella storage plugins.
JavaScript
2
star
28

express-snapshot

Generate static HTML from Express. Express app in, static .html out.
JavaScript
2
star
29

zero.css

Classy style without classes
CSS
1
star
30

daily-standup

Command-line tool for posting what you did today and the GitHub commits from yesterday to HipChat.
JavaScript
1
star