|
| 1 | +koa-magic |
| 2 | +========================================= |
| 3 | +Supercharged, Ready-to-go, Web-Application Server |
| 4 | + |
| 5 | +## Features ## |
| 6 | + |
| 7 | +* Stackable routing middleware |
| 8 | +* Path-Match Router |
| 9 | +* VHost Router (domain/hostname based) |
| 10 | +* Easy to use application server including basic koa modules |
| 11 | +* Components can be used standalone |
| 12 | +* Cluster support/Hot-Reload via [cluster-magic](https://github.com/AndiDittrich/Node.cluster-magic) |
| 13 | + |
| 14 | +## Install ## |
| 15 | + |
| 16 | +```bash |
| 17 | +$ npm install koa-magic --save |
| 18 | +$ yarn add koa-magic |
| 19 | +``` |
| 20 | + |
| 21 | +## Modules ## |
| 22 | + |
| 23 | +koa-magic provides the following modules |
| 24 | + |
| 25 | +* [Koa](docs/koa.md) - Extended Koa class including routing methods `require('koa-magic').Koa` |
| 26 | +* [Router](docs/router.md) - Stackable - express like - routing middleware `require('koa-magic').Router` |
| 27 | +* [ApplicationServer](docs/application-server.md) - Ready-to-use Koa webserver including basic middleware (compress, sessions) `require('koa-magic').ApplicationServer` |
| 28 | + |
| 29 | +## Docs ## |
| 30 | + |
| 31 | +TBD |
| 32 | + |
| 33 | +## Router ## |
| 34 | + |
| 35 | +The path is parsed by [path-to-regexp](https://github.com/pillarjs/path-to-regexp) to enable parametric paths. The parameters are exposed by the `ctx.params` object. |
| 36 | + |
| 37 | +### Methods ## |
| 38 | + |
| 39 | +The following methods are provided by each routing instance (or extended Koa class) |
| 40 | + |
| 41 | +**HTTP** |
| 42 | + |
| 43 | +* `get(path:string|RegExp, middleware:Promise)` handle http-get |
| 44 | +* `post(path:string|RegExp, middleware:Promise)` handle http-post |
| 45 | +* `put(path:string|RegExp, middleware:Promise)` handle http-put |
| 46 | +* `head(path:string|RegExp, middleware:Promise)` handle http-head |
| 47 | +* `delete(path:string|RegExp, middleware:Promise)` handle http-delete |
| 48 | +* `options(path:string|RegExp, middleware:Promise)` handle http-options |
| 49 | +* `all(path:string|RegExp, middleware:Promise)` handle all kind of http-methods |
| 50 | + |
| 51 | +**VHOST** |
| 52 | + |
| 53 | +* `vhost(hostname:string|RegExp, middleware:Promise|Router`) virtual host routing |
| 54 | + |
| 55 | +**ROUTING** |
| 56 | + |
| 57 | +* `attach(path:string|RegExp, middleware:Promise|Router`) add a new router to the current stack (like use) |
| 58 | + |
| 59 | +### Examples ### |
| 60 | + |
| 61 | +**Example 1 - path routing** |
| 62 | +```js |
| 63 | +const Router = require('koa-magic').Router; |
| 64 | +const Koa = require('koa-magic').Koa; |
| 65 | + |
| 66 | +// initialize koa + router |
| 67 | +const webapp = new Koa(); |
| 68 | +const myrouter = new Router(); |
| 69 | + |
| 70 | +myrouter.get('/world.html', ctx => { |
| 71 | + ctx.body = "ook"; |
| 72 | +}); |
| 73 | +myrouter.post('/world.html', ctx => { |
| 74 | + ctx.body = "ook"; |
| 75 | +}); |
| 76 | + |
| 77 | +// attach to root path |
| 78 | +koa.get('/helloworld.html' ctx => { |
| 79 | + ctx.body = 'Helloworld'; |
| 80 | +}); |
| 81 | + |
| 82 | +// attach router middleware - use attach() of extended Koa! |
| 83 | +koa.attach('/hello', myrouter); |
| 84 | + |
| 85 | +// start server |
| 86 | +koa.listen(...); |
| 87 | +``` |
| 88 | + |
| 89 | +**Example 2 - vhost routing** |
| 90 | +```js |
| 91 | +const Router = require('koa-magic').Router; |
| 92 | +const Koa = require('koa-magic').Koa; |
| 93 | + |
| 94 | +// initialize koa + router |
| 95 | +const webapp = new Koa(); |
| 96 | +const myrouter = new Router(); |
| 97 | + |
| 98 | +myrouter.get('/world.html', ctx => { |
| 99 | + ctx.body = "ook"; |
| 100 | +}); |
| 101 | + |
| 102 | +// attach router middleware to domain |
| 103 | +koa.vhost('example.org', myrouter); |
| 104 | + |
| 105 | +// start server |
| 106 | +koa.listen(...); |
| 107 | +``` |
| 108 | + |
| 109 | +## License ## |
| 110 | +koa-magic is OpenSource and licensed under the Terms of [The MIT License (X11)](http://opensource.org/licenses/MIT) - your're welcome to contribute |
0 commit comments