koa-controller (Space Version)
Iyobo's Note:
In this fork , I replaced the '#' controller/action delimetter with empty space (i.e. to: 'controller action') and also removed unneccessary, restrictive code to determine if a route is a redirect vs a controller. These changes allow for more flexible redirection routes, as you previously could not make redirects like '/dashboard#messages' due to fragment urls being previously treated as controllers.
You don't even need to add root domains or domain protocols to redirects anymore e.g http://domain.com, https://, etc.
Koa-controller
in a middleware for Koa
which handles the routing of your application where related functionalities are splitted into routes
, controllers
and constraints
. The module is built on top of koa-route middleware. It optimizes your code and brings the following features into your project:
- Flexible
routes
handler with a single point of router configuration. - Application
controllers
for handling application responses. - Access control middleware with
constraints
for limiting requests to application controllers, handling user authentication and security. - Context tools for easy dynamic data manipulation.
Installation
Install the npm package.
npm install koa-controller --save
Attach the middleware.
var koa = ;var app = ;var kc = ;app; // optionalapp;app;
By default the middleware expects that controllers exist at app/controllers/{controller}.js
, constraints at app/constraints/{constraint}.js
and the router configuration file at config/routes.js
. We can easily change the default behavior as shown bellow.
app;
Note that routesPath
and controllerPath
must exist where constraintPath
is not required.
Routes
Routes file is a simple key-value object where the key
represents a route
and the value
represents a task
. Create a new file and define your project's routes based on the example bellow.
// config/routes.jsmoduleexports = // controller#action '/users/:id?': to: 'users#find' 'post /users': to: 'users#create' 'put|post /users/:id': to: 'users#update' 'get /users/:id/words/:slug*': to: 'events#words' 'get /event/:slug+': to: 'events#index' constraint: 'api#ip' // redirections 'get /to/google': to: 'http://www.google.com' 'get /to/home': to: '/' // using a function 'get /events/:id': { thisbody = ... } ...;
You check koa-route and path-to-regexp for more information.
Controller
Controller is a simple key-value object where the key
represents the name of an action
and the value
represents a generator function that processes the request. Create a new file for your first controller and define actions based on the example bellow. Don't forget to connect the new controller with a route inside routes.js
file.
// app/controllers/users.jsmoduleexports = { thisbody = ...; } { } { } ...;
Notice the this.body
call? Every action
inside a controller has access to Koa context. Check koa-route for details.
Constraint
Constraint is a simple key-value object where the key
represents the name of a constraint and the value
represents a generator function that processes the request. Create a new file for your first constraint and define constraints based on the example bellow. Don't forget to connect the new constraint with a route inside routes.js
file.
// app/constraints/api.jsmoduleexports = { if thisrequestip == '192.168.1.100' // allow access only from this IP address next; else thisbody = 'Unauthorized IP address'; thisstatus = 401; } ...;
Note that constraints are very much like controllers thus every constraint action has access to Koa context. Check koa-route for details.
Tools
By attaching kc.tools()
middleware the context features are extended.
ctx.form([names])
Type: Function
Returns: Object
Parsed request body data. You can retrive only selected attributes by specifying a list of names
.
console;// -> { 'name': 'John', 'email': 'john@gmail.com', 'age': 33 }console;// -> { 'name': 'John', 'age': 33 }