Skip to content

Commit f1ee3c7

Browse files
committed
added routing middleware
1 parent 2f2e865 commit f1ee3c7

19 files changed

+737
-345
lines changed

CONTRIBUTING.md

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Contribution
22
------------
33

4-
http-magic is OpenSource and managed on [GitHub](https://github.com/) - if you like, you're welcome to contribute!
4+
koa-magic is OpenSource and managed on [GitHub](https://github.com/AndiDittrich/Node.koa-magic) - if you like, you're welcome to contribute!
55
To simplify the release and quality control process, please follow these remarks:
66

77
### Notices ###
88
* Related software packages are updated by the maintainer
9-
* If you form a concept of **larger project changes**, please [discuss]() them with the contributors **before** implementing
9+
* If you form a concept of **larger project changes**, please [discuss](https://github.com/AndiDittrich/Node.koa-magic/issues) them with the contributors **before** implementing

LICENSE.md

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License
22

3-
Copyright (c) 2018 Andi Dittrich
3+
Copyright (c) 2017-2018 Andi Dittrich
44

55
Permission is hereby granted, free of charge, to any person
66
obtaining a copy of this software and associated documentation

RAEDME.md

Lines changed: 0 additions & 25 deletions
This file was deleted.

README.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

docs/cluster.md

Lines changed: 0 additions & 65 deletions
This file was deleted.

docs/router.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
router
2+
===============================
3+
4+
https://github.com/koajs/compose

http-magic.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

koa-magic.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const _router = require('./lib/koa-router');
2+
const _koa = require('./lib/koa-extend');
3+
const _applicationServer = require('./lib/koa-application-server');
4+
5+
module.exports = {
6+
Router: _router,
7+
Koa: _koa,
8+
ApplicationServer: _applicationServer
9+
};

0 commit comments

Comments
 (0)