UNPKG

4.81 kBMarkdownView Raw
1koa-middlewares
2===============
3
4[![Dependency Status](https://gemnasium.com/cnpm/koa-middlewares.svg)](https://gemnasium.com/dead-horse/koa-middlewares)
5
6easy 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```
13npm 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
37see [exports](index.js)
38
39## Usage
40
41```js
42
43var koa = require('koa');
44var middlewares = require('koa-middlewares');
45
46var app = koa();
47
48app.use(middlewares.bodyParser());
49app.use(middlewares.router(app));
50app.use(middlewares.conditional());
51app.use(middlewares.etag());
52app.use(middlewares.compress());
53middlewares.csrf(app);
54
55app.use(function *() {
56 this.body = 'hello koa-middlewares';
57});
58
59app.listen(7001);
60```
61
62## Middlewares Quick Guide
63
64* **koa-bodyparser**: post body parser,
65for `application/json` and `application/x-www-form-urlencoded`.
66
67```
68app.use(middlewares.bodyParser({
69 limit: '10mb'
70}));
71
72app.use(function *(next) {
73 var postBody = this.request.body;
74});
75```
76
77* **koa-csrf**: CSRF tokens.
78
79```
80middlewares.csrf(app);
81app.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```
96middlewares.render(app, {
97 root: path.join(__dirname, 'view')
98});
99
100app.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.
107use it upstream from etag.
108
109```
110app.use(middlewares.conditional());
111app.use(middlewares.etag());
112```
113
114* **koa-favicon**: Bounce favicon requests with a 404.
115
116```
117app.use(middlewares.favicon());
118```
119
120* **koa-safe-jsonp**: A safe jsonp plugins for koa.
121
122```
123middlewares.jsonp(app);
124
125app.use(function* () {
126 this.jsonp = {foo: 'bar'};
127});
128```
129
130* **koa-logger**: Development style logger.
131
132```
133app.use(middlewares.logger());
134```
135
136* **koa-session**: cookie base session.
137
138```
139app.use(middlewares.cookieSession());
140```
141
142* **koa-generic-session**: A session like connect with memory,
143has 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```
147app.use(middlewares.session({
148 store: middlewares.RedisStore(),
149 defer: true
150}));
151
152app.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```
162app.use(middlewares.router(app));
163app.get('/', function *() {
164 this.body = 'Hello koa-router';
165});
166```
167
168* **koa-resource-router**: RESTful resource routing for koa.
169
170```
171var users = new middlewares.Resource('users');
172app.use(users.middleware());
173
174app.get('/users', function *() {
175 this.body = [{name: 'Lee'}, {name: 'Han'}];
176});
177```
178
179* **koa-rewrite**: URL rewrite middleware.
180
181```
182app.use(middlewares.rewrite('/js/*', '/public/assets/js/$1'));
183```
184
185* **koa-rt**: Log response time, support custom with microtime.
186
187```
188var microtime = require('microtime');
189app.use(middlewares.rt({
190 timer: microtime
191}));
192```
193
194* **koa-static-cache**: Static file serving from memory.
195
196```
197app.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```
207var app = koa()
208app.use(compress({
209 threshold: 2048,
210 flush: require('zlib').Z_SYNC_FLUSH
211}))
212```
213
214* **koa-onerror**: Error handler
215
216```
217var app = koa()
218onerror(app);
219```
220
221## License
222MIT