1 | koa-middlewares
|
2 | ===============
|
3 |
|
4 | [![Dependency Status](https://gemnasium.com/cnpm/koa-middlewares.svg)](https://gemnasium.com/dead-horse/koa-middlewares)
|
5 |
|
6 | easy way to use some small but useful koa middlewares.
|
7 |
|
8 | **PRs** are welcome, but only for those small and general middlewares.
|
9 |
|
10 | ## install
|
11 |
|
12 | ```
|
13 | npm install koa-middlewares --save
|
14 | ```
|
15 |
|
16 | ## middlewares
|
17 |
|
18 | * [koa-bodyparser](https://github.com/dead-horse/koa-body-parser)
|
19 | * [koa-compress](https://github.com/koajs/koa-compress)
|
20 | * [koa-conditional-get](https://github.com/koajs/koa-conditional-get)
|
21 | * [koa-csrf](https://github.com/koajs/csrf)
|
22 | * [koa-ejs](https://github.com/dead-horse/koa-ejs)
|
23 | * [koa-etag](https://github.com/koajs/etag)
|
24 | * [koa-favicon](https://github.com/koajs/favicon)
|
25 | * [koa-generic-session](https://github.com/koajs/generic-session)
|
26 | * [koa-logger](https://github.com/koajs/logger)
|
27 | * [koa-onerror](https://github.com/koajs/onerror)
|
28 | * [koa-redis](https://github.com/dead-horse/koa-redis)
|
29 | * [koa-resource-router](https://github.com/alexmingoia/koa-resource-router)
|
30 | * [koa-rewrite](https://github.com/koajs/rewrite)
|
31 | * [koa-router](https://github.com/alexmingoia/koa-router)
|
32 | * [koa-rt](https://github.com/dead-horse/koa-rt)
|
33 | * [koa-safe-jsonp](https://github.com/koajs/koa-safe-jsonp)
|
34 | * [koa-session](https://github.com/koajs/session)
|
35 | * [koa-static-cache](https://github.com/koajs/static-cache)
|
36 |
|
37 | see [exports](index.js)
|
38 |
|
39 | ## Usage
|
40 |
|
41 | ```js
|
42 |
|
43 | var koa = require('koa');
|
44 | var middlewares = require('koa-middlewares');
|
45 |
|
46 | var app = koa();
|
47 |
|
48 | app.use(middlewares.bodyParser());
|
49 | app.use(middlewares.router(app));
|
50 | app.use(middlewares.conditional());
|
51 | app.use(middlewares.etag());
|
52 | app.use(middlewares.compress());
|
53 | middlewares.csrf(app);
|
54 |
|
55 | app.use(function *() {
|
56 | this.body = 'hello koa-middlewares';
|
57 | });
|
58 |
|
59 | app.listen(7001);
|
60 | ```
|
61 |
|
62 | ## Middlewares Quick Guide
|
63 |
|
64 | * **koa-bodyparser**: post body parser,
|
65 | for `application/json` and `application/x-www-form-urlencoded`.
|
66 |
|
67 | ```
|
68 | app.use(middlewares.bodyParser({
|
69 | limit: '10mb'
|
70 | }));
|
71 |
|
72 | app.use(function *(next) {
|
73 | var postBody = this.request.body;
|
74 | });
|
75 | ```
|
76 |
|
77 | * **koa-csrf**: CSRF tokens.
|
78 |
|
79 | ```
|
80 | middlewares.csrf(app);
|
81 | app.use(function *checkCsrf(next) {
|
82 | if (this.method === 'GET' ||
|
83 | this.method === 'HEAD' ||
|
84 | this.method === 'OPTIONS') {
|
85 | return yield *next;
|
86 | }
|
87 |
|
88 | this.assertCsrf();
|
89 | yield next;
|
90 | });
|
91 | ```
|
92 |
|
93 | * **koa-ejs**: ejs view render middleware. support all feature of ejs.
|
94 |
|
95 | ```
|
96 | middlewares.render(app, {
|
97 | root: path.join(__dirname, 'view')
|
98 | });
|
99 |
|
100 | app.use(function *() {
|
101 | yield this.render('page.html', {foo: 'bar'});
|
102 | });
|
103 | ```
|
104 |
|
105 | * **koa-etag**: ETag support for Koa responses.
|
106 | * **koa-conditional-get**: HTTP response freshness testing middleware base on node-fresh.
|
107 | use it upstream from etag.
|
108 |
|
109 | ```
|
110 | app.use(middlewares.conditional());
|
111 | app.use(middlewares.etag());
|
112 | ```
|
113 |
|
114 | * **koa-favicon**: Bounce favicon requests with a 404.
|
115 |
|
116 | ```
|
117 | app.use(middlewares.favicon());
|
118 | ```
|
119 |
|
120 | * **koa-safe-jsonp**: A safe jsonp plugins for koa.
|
121 |
|
122 | ```
|
123 | middlewares.jsonp(app);
|
124 |
|
125 | app.use(function* () {
|
126 | this.jsonp = {foo: 'bar'};
|
127 | });
|
128 | ```
|
129 |
|
130 | * **koa-logger**: Development style logger.
|
131 |
|
132 | ```
|
133 | app.use(middlewares.logger());
|
134 | ```
|
135 |
|
136 | * **koa-session**: cookie base session.
|
137 |
|
138 | ```
|
139 | app.use(middlewares.cookieSession());
|
140 | ```
|
141 |
|
142 | * **koa-generic-session**: A session like connect with memory,
|
143 | has friendly APIs for work with other Stores such as `redis`, `mongo`.
|
144 | * **koa-redis**: Work togather with `koa-generic-session`, provide a redis store from koa-sess.
|
145 |
|
146 | ```
|
147 | app.use(middlewares.session({
|
148 | store: middlewares.RedisStore(),
|
149 | defer: true
|
150 | }));
|
151 |
|
152 | app.use(function *() {
|
153 | var session = yield this.session;
|
154 | session.foo = 'bar';
|
155 | this.body = this.session.foo;
|
156 | });
|
157 | ```
|
158 |
|
159 | * **koa-router**: Provide express-style routing using app.get, app.put, app.post.
|
160 |
|
161 | ```
|
162 | app.use(middlewares.router(app));
|
163 | app.get('/', function *() {
|
164 | this.body = 'Hello koa-router';
|
165 | });
|
166 | ```
|
167 |
|
168 | * **koa-resource-router**: RESTful resource routing for koa.
|
169 |
|
170 | ```
|
171 | var users = new middlewares.Resource('users');
|
172 | app.use(users.middleware());
|
173 |
|
174 | app.get('/users', function *() {
|
175 | this.body = [{name: 'Lee'}, {name: 'Han'}];
|
176 | });
|
177 | ```
|
178 |
|
179 | * **koa-rewrite**: URL rewrite middleware.
|
180 |
|
181 | ```
|
182 | app.use(middlewares.rewrite('/js/*', '/public/assets/js/$1'));
|
183 | ```
|
184 |
|
185 | * **koa-rt**: Log response time, support custom with microtime.
|
186 |
|
187 | ```
|
188 | var microtime = require('microtime');
|
189 | app.use(middlewares.rt({
|
190 | timer: microtime
|
191 | }));
|
192 | ```
|
193 |
|
194 | * **koa-static-cache**: Static file serving from memory.
|
195 |
|
196 | ```
|
197 | app.use(middlewares.staticCache(path.join(__dirname, 'public'), {
|
198 | buffer: true,
|
199 | maxAge: 60 * 60 * 24 * 7,
|
200 | dir: path.join(rootdir, 'public')
|
201 | }));
|
202 | ```
|
203 |
|
204 | * **koa-compress**: Compress middleware for Koa, support `gzip` and `deflate`
|
205 |
|
206 | ```
|
207 | var app = koa()
|
208 | app.use(compress({
|
209 | threshold: 2048,
|
210 | flush: require('zlib').Z_SYNC_FLUSH
|
211 | }))
|
212 | ```
|
213 |
|
214 | * **koa-onerror**: Error handler
|
215 |
|
216 | ```
|
217 | var app = koa()
|
218 | onerror(app);
|
219 | ```
|
220 |
|
221 | ## License
|
222 | MIT
|