New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

koa-controller-router

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-controller-router

> Router middleware extended from [koa-router](https://github.com/alexmingoia/koa-router) for [koa 1.x](https://github.com/koajs/koa/tree/v1.x)

  • 0.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9
decreased by-40%
Maintainers
1
Weekly downloads
 
Created
Source

koa-controller-router

Router middleware extended from koa-router for koa 1.x

Installation

Install using npm:

npm install koa-controller-router

Usage example

Create controller

// app/controllers/ArticleController.js

class ArticleController {
    *list() {
        // some code
    }
    
    *show() {
        // some code
    }
    
    *create() {
        // some code
    }

    *update() {
        // some code
    }

    *destroy() {
        // some code
    }
}

module.exports = ArticleController;

Create routes

Without middleware:

// index.js
const path = require('path');
const Koa = require('koa');
const Router = require('koa-controller-router');

const app = new Koa();
const router = new Router({
  controllersPath: path.resolve(__dirname, 'app', 'controllers') // default value is /path/to/project/controllers/
});

router.get('/', function *() {
    // some code
});

router.get('/api/v1/articles', 'ArticleController@list');
router.get('/api/v1/articles/:id', 'ArticleController@show');
router.post('/api/v1/articles', 'ArticleController@create');
router.put('/api/v1/articles/:id', 'ArticleController@update');
router.patch('/api/v1/articles/:id', 'ArticleController@update');
router.del('/api/v1/articles', 'ArticleController@destroy');

app.use(router.routes());
app.use(router.allowedMethods());

app.listen(3000);

With middleware:

const authMiddleware = function *(next) {
    // auth check
    
    yield next;
};

const isAdminMiddleware = function *(next) {
    // check user role
    
    yield next;
};

router.post('/api/v1/articles', authMiddleware, 'ArticleController@create');
router.put('/api/v1/articles/:id', authMiddleware, 'ArticleController@update');
router.patch('/api/v1/articles/:id', authMiddleware, 'ArticleController@update');
router.del('/api/v1/articles', authMiddleware, isAdminMiddleware, 'ArticleController@destroy');

Licence

MIT

Keywords

FAQs

Package last updated on 30 Mar 2017

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc