{"id":16981970,"url":"https://github.com/1602/kontroller","last_synced_at":"2025-04-12T02:21:01.612Z","repository":{"id":4983333,"uuid":"6141320","full_name":"1602/kontroller","owner":"1602","description":"Generic Controller for express / compound / socket.io and everything else","archived":false,"fork":false,"pushed_at":"2016-07-04T18:18:58.000Z","size":67,"stargazers_count":7,"open_issues_count":6,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-25T22:01:33.674Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/1602.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-10-09T13:42:56.000Z","updated_at":"2025-01-02T01:13:55.000Z","dependencies_parsed_at":"2022-09-17T04:40:55.544Z","dependency_job_id":null,"html_url":"https://github.com/1602/kontroller","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1602%2Fkontroller","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1602%2Fkontroller/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1602%2Fkontroller/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1602%2Fkontroller/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/1602","download_url":"https://codeload.github.com/1602/kontroller/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248505921,"owners_count":21115354,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-14T02:06:58.809Z","updated_at":"2025-04-12T02:21:01.579Z","avatar_url":"https://github.com/1602.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## About\n\nControllers used in CompoundJS. Could be used separately. This package provides\nefficient way of describing controllers, it works insanely fast (sometimes\neven faster then manually written express controller). Optimized for high\nperformance.\n\n## Installation\n\n    npm install kontroller\n\n## Basic usage (ExpressJS example)\n\n    // create some controller\n    var Driver = require('kontroller').BaseController.constructClass();\n\n    // define action\n    Driver.actions.accelerate = function accelerate(c) {\n        c.send('accelerating!');\n    };\n\n    // and another one\n    Driver.actions.brake = function brake(c) {\n        c.send('braking!');\n    };\n\n    // now let's create express app\n    var express = require('express);\n    var app = express();\n\n    // and map routes to controller\n    app.get('/speedup', Driver('accelerate'));\n    app.get('/slowdown', Driver('brake'));\n\n    // run, test\n    app.listen(3000);\n\n## Context-free controllers\n\nThere are a lot of hidden features in your new controller. You can rewrite it in\ncompoundjs-style:\n\n    before(think, {only: 'accelerate'});\n\n    action('accelerate', function () {\n        send('accelerating!');\n    });\n\n    action('brake', function () {\n        send('braking!');\n    });\n\n    function think() {\n        // think 1 second before accelerate\n        setTimeout(next, 1000);\n    }\n\nAnd use as compoundjs does it:\n\n    // create blank controller\n    var Driver = BaseController.constructClass('Driver');\n\n    // instantiate and configure\n    var ctl = new Driver;\n    ctl.build(code); // code is string, containing code of controller\n\n    // feed to express\n    app.get('/speedup', Driver('accelerate'));\n    app.get('/slowdown', Driver('brake'));\n\n## Respond to specific format:\n\n    action(function index() {\n        var fruits = this.fruits = ['apple', 'banana', 'kiwi'];\n        respondTo(function (format) {\n            format.html(render);\n            format.json(function () {\n                send(fruits);\n            });\n        });\n    });\n\nExtend list of formats:\n\n    require('kontroller').Helpers.respondToFormats.push('gif', 'png', 'jpg');\n\nAnd then you can use it:\n\n    action(function image() {\n        respondTo(function (format) {\n            format.png(renderPNG);\n            format.jpg(renderJPG);\n            format.git(renderGIF);\n        });\n        // note: methods renderGIF, renderJPG, renderPNG aren't part of\n        // kontroller, this is just some named functions\n        // used for example\n    });\n\n## Flow control\n\nYou can use hooks for specific actions / all actions\n\n    before(function () {\n        next(); // call next to get to the next hook/action\n    });\n\nNext hook only will be executed when action is `specialCase`:\n\n    before(function doSmthSpecial() {\n        next();\n    }, {only: 'specialCase'});\n\nNext hook only will be executed when action is not `publicResource`:\n\n    before(function authorizeUser() {\n        if (!session.user) {\n            redirect('/login');\n        } else {\n            next();\n        }\n    }, {except: 'publicResource'});\n\nYou also can pass array of strings as `only` and `except` settings when you need\nto hook multiple actions:\n\n    // load resource before actions: show, edit, update, destroy\n    before(loadResource, {only: ['show', 'edit', 'update', 'destroy']});\n\n    // only blahBlah if action is not pipa or moka\n    before(blahBlah, {except: ['pipa', 'moka']});\n\n## Controllers pool\n\nControllers produced via `kontroller` designed to be fast as possible. Every\ncontroller instance could be reused multiple times (to save time for it's\ninstantiation and reduce memory usage). This trick allows to save same\nperformance level as it would be for just function, and not separate object\n\nThe only problem here in concurrent requests. Obviously, concurrent requests\nshould be handled using different controller instances, so we have pool for\ncontrollers. When request comes, firts of all we trying to pull controller from\nthe pool, and only if pool is empty - creating new instance. After request\nhandling is completed - controller instance pushed back to pool.\n\n# MIT License\n\n    Copyright (C) 2011 by Anatoliy Chakkaev\n\n    Permission is hereby granted, free of charge, to any person obtaining a copy\n    of this software and associated documentation files (the \"Software\"), to deal\n    in the Software without restriction, including without limitation the rights\n    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n    copies of the Software, and to permit persons to whom the Software is\n    furnished to do so, subject to the following conditions:\n\n    The above copyright notice and this permission notice shall be included in\n    all copies or substantial portions of the Software.\n\n    THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n    THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F1602%2Fkontroller","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F1602%2Fkontroller","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F1602%2Fkontroller/lists"}