New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

koa-simple-controller

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-simple-controller

Create simple controllers for use with the Koa.js framework on Node.js

latest
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

koa-simple-controller

Create simple controllers for use with the Koa.js framework on Node.js

  • Router agnostic controller, allows you to integrate into multiple router modules such as koa-route or koa-router
  • Attaches controllers/actions to Koa context, and to controller object

Installation

Install using npm:

npm install koa-simple-controller --save

Usage

server.js


var app = require('koa')();
var router = require('koa-router')();
// Specify path to controller directory in option passed to koa-simple-controller
// Defaults to './controller' if none specified, e.g. require('koa-simple-controller')();
var controller = require('koa-simple-controller')('./app/controller/');

// Method initalizes the controller object and attached controllers to the Koa context and controller object
app.use(controller.initialize());

// koa-router routes as example
router.get('/example', function* (next){
    // Here's the Koa context
    var ctx = this;
    // Pass context to controller, i.e. this
    ctx.controllers.test.index(ctx);
    yield* next;
});

app.use(router.routes());

app.listen(3000);

./app/controller/test.js

Each controller is an exported object consisting of methods that serve as actions. File names are used as controller names.


// Export each set of actions 
module.exports = {
    index : function (ctx) {
        ctx.body = 'Hello.';
    },
    test : function(ctx) {
        ctx.body = 'Testing';
    }
};

Keywords

koa

FAQs

Package last updated on 06 Jul 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts