1 |
|
2 | # koa-mount
|
3 |
|
4 | Mount other Koa applications as middleware. The `path` passed to `mount()` is stripped
|
5 | from the URL temporarily until the stack unwinds. This is useful for creating entire
|
6 | apps or middleware that will function correctly regardless of which path segment(s)
|
7 | they should operate on.
|
8 |
|
9 | ## Installation
|
10 |
|
11 | ```js
|
12 | $ npm install koa-mount
|
13 | ```
|
14 |
|
15 | ## Examples
|
16 |
|
17 | View the [./examples](blob/master/examples) directory for working examples.
|
18 |
|
19 | ### Mounting Applications
|
20 |
|
21 | Entire applications mounted at specific paths. For example you could mount
|
22 | a blog application at "/blog", with a router that matches paths such as
|
23 | "GET /", "GET /posts", and will behave properly for "GET /blog/posts" etc
|
24 | when mounted.
|
25 |
|
26 | ```js
|
27 | var mount = require('koa-mount');
|
28 | var koa = require('koa');
|
29 |
|
30 | // hello
|
31 |
|
32 | var a = koa();
|
33 |
|
34 | a.use(function *(next){
|
35 | yield next;
|
36 | this.body = 'Hello';
|
37 | });
|
38 |
|
39 | // world
|
40 |
|
41 | var b = koa();
|
42 |
|
43 | b.use(function *(next){
|
44 | yield next;
|
45 | this.body = 'World';
|
46 | });
|
47 |
|
48 | // app
|
49 |
|
50 | var app = koa();
|
51 |
|
52 | app.use(mount('/hello', a));
|
53 | app.use(mount('/world', b));
|
54 |
|
55 | app.listen(3000);
|
56 | console.log('listening on port 3000');
|
57 | ```
|
58 |
|
59 | Try the following requests:
|
60 |
|
61 | ```
|
62 | $ GET /
|
63 | Not Found
|
64 |
|
65 | $ GET /hello
|
66 | Hello
|
67 |
|
68 | $ GET /world
|
69 | World
|
70 | ```
|
71 |
|
72 | ### Mounting Middleware
|
73 |
|
74 | Mount middleware at specific paths, allowing them to operate independently
|
75 | of the prefix, as they're not aware of it.
|
76 |
|
77 | ```js
|
78 | var mount = require('koa-mount');
|
79 | var koa = require('koa');
|
80 |
|
81 | function *hello(next){
|
82 | yield next;
|
83 | this.body = 'Hello';
|
84 | }
|
85 |
|
86 | function *world(next){
|
87 | yield next;
|
88 | this.body = 'World';
|
89 | }
|
90 |
|
91 | var app = koa();
|
92 |
|
93 | app.use(mount('/hello', hello));
|
94 | app.use(mount('/world', world));
|
95 |
|
96 | app.listen(3000);
|
97 | console.log('listening on port 3000');
|
98 | ```
|
99 |
|
100 | ### Optional Paths
|
101 |
|
102 | The path argument is optional, defaulting to "/":
|
103 |
|
104 | ```js
|
105 | app.use(mount(a));
|
106 | app.use(mount(b));
|
107 | ```
|
108 |
|
109 | ## Debugging
|
110 |
|
111 | Use the __DEBUG__ environement variable to whitelist
|
112 | koa-mount debug output:
|
113 |
|
114 | ```
|
115 | $ DEBUG=koa-mount node myapp.js &
|
116 | $ GET /foo/bar/baz
|
117 |
|
118 | koa-mount enter /foo/bar/baz -> /bar/baz +2s
|
119 | koa-mount enter /bar/baz -> /baz +0ms
|
120 | koa-mount enter /baz -> / +0ms
|
121 | koa-mount leave /baz -> / +1ms
|
122 | koa-mount leave /bar/baz -> /baz +0ms
|
123 | koa-mount leave /foo/bar/baz -> /bar/baz +0ms
|
124 | ```
|
125 |
|
126 | ## License
|
127 |
|
128 | MIT |
\ | No newline at end of file |