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

koa-controller-jollof

v1.1.2

Published

Middleware for Koa which handles the routing of your application were related functionalities are splitted into routes, controllers and constraints.

Downloads

10

Readme

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.

Build Status NPM version Dependency Status

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 = require('koa');
var app = koa();
var kc = require('koa-controller');
app.use(kc.tools()); // optional
app.use(kc.router());
app.listen(3000);

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.use(controller({
  routesPath: 'my/path/routes.js',
  controllerPath: 'my/controllers/{controller}.js', // note that {controller} is a variable
  constraintPath: 'my/constraints/{constraint}.js', // note that {constraint} is a variable
  logger: console.log // custom logger function
}));

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.js
module.exports = {

  // 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': { to: function *(id) { this.body = ... } },

  ...
};

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.js
module.exports = {

  find: function*() {
    this.body = ...;
  },

  update: function*(id) {
  },

  words: function*(id, slug) {
  },

  ...
};

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.js
module.exports = {

  ip: function*(next) {
    if (this.request.ip == '192.168.1.100') { // allow access only from this IP address
      yield next;
    } else {
      this.body = 'Unauthorized IP address';
      this.status = 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.log( _.form() );
// -> { 'name': 'John', 'email': '[email protected]', 'age': 33 }
console.log( _.form('name', 'age') );
// -> { 'name': 'John', 'age': 33 }