• This repository has been archived on 06/Jan/2023
  • Stars
    star
    177
  • Rank 215,985 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

easy way to use some useful koa middlewares

koa-middlewares

NPM version build status David deps Known Vulnerabilities npm download

easy way to use some small but useful koa middlewares.

PRs are welcome, but only for those small and general middlewares.

install

npm install koa-middlewares --save

middlewares

see exports

Usage

var koa = require('koa');
var middlewares = require('koa-middlewares');
var router = middlewares.router();

var app = koa();

router.get('/', function *(){
  this.body = 'hello koa-middlewares';
});

app.use(middlewares.bodyParser());
app.use(middlewares.conditional());
app.use(middlewares.etag());
app.use(middlewares.compress());
middlewares.csrf(app);
app.use(router.routes());
app.use(router.allowedMethods());

app.listen(7001);

Middlewares Quick Guide

  • koa-bodyparser: post body parser, for application/json and application/x-www-form-urlencoded.
app.use(middlewares.bodyParser({
  limit: '10mb'
}));

app.use(function *(next) {
  var postBody = this.request.body;
});
  • koa-csrf: CSRF tokens.
middlewares.csrf(app);
app.use(function *checkCsrf(next) {
  if (this.method === 'GET' ||
      this.method === 'HEAD' ||
      this.method === 'OPTIONS') {
    return yield *next;
  }

  this.assertCsrf();
  yield next;
});
  • koa-ejs: ejs view render middleware. support all feature of ejs.
middlewares.render(app, {
  root: path.join(__dirname, 'view')
});

app.use(function *() {
  yield this.render('page.html', {foo: 'bar'});
});
  • koa-etag: ETag support for Koa responses.
  • koa-conditional-get: HTTP response freshness testing middleware base on node-fresh. use it upstream from etag.
app.use(middlewares.conditional());
app.use(middlewares.etag());
  • koa-favicon: Bounce favicon requests with a 404.
app.use(middlewares.favicon());
  • koa-safe-jsonp: A safe jsonp plugins for koa.
middlewares.jsonp(app);

app.use(function* () {
  this.jsonp = {foo: 'bar'};
});
  • koa-logger: Development style logger.
app.use(middlewares.logger());
  • koa-session: cookie base session.
app.use(middlewares.cookieSession());
  • koa-generic-session: A session like connect with memory, has friendly APIs for work with other Stores such as redis, mongo.
  • koa-redis: Work togather with koa-generic-session, provide a redis store from koa-sess.
app.use(middlewares.session({
  store: middlewares.RedisStore(),
  defer: true
}));

app.use(function *() {
  var session = yield this.session;
  session.foo = 'bar';
  this.body = this.session.foo;
});
  • koa-router: Provide express-style routing using app.get, app.put, app.post.
var router = middlewares.router();

router.get('/', function *() {
  this.body = 'Hello koa-router';
});

app.use(router.routes());
app.use(router.allowedMethods());
  • koa-resource-router: RESTful resource routing for koa.
var users = new middlewares.Resource('users');
app.use(users.middleware());

app.get('/users', function *() {
  this.body = [{name: 'Lee'}, {name: 'Han'}];
});
  • koa-rewrite: URL rewrite middleware.
app.use(middlewares.rewrite('/js/*', '/public/assets/js/$1'));
  • koa-rt: Log response time, support custom with microtime.
var microtime = require('microtime');
app.use(middlewares.rt({
  timer: microtime
}));
  • koa-static-cache: Static file serving from memory.
app.use(middlewares.staticCache(path.join(__dirname, 'public'), {
  buffer: true,
  maxAge: 60 * 60 * 24 * 7,
  dir: path.join(rootdir, 'public')
}));
  • koa-compress: Compress middleware for Koa, support gzip and deflate
var app = koa()
app.use(compress({
  threshold: 2048,
  flush: require('zlib').Z_SYNC_FLUSH
}))
  • koa-onerror: Error handler
var app = koa()
onerror(app);

License

MIT

More Repositories

1

cnpmjs.org

‼️ ‼️ ‼️ ‼️ DEPRECATED, please use https://github.com/cnpm/cnpmcore ‼️ ‼️ ‼️ ‼️
JavaScript
3,551
star
2

cnpm

cnpm: npm client for China mirror of npm
JavaScript
2,016
star
3

cnpmcore

Private NPM Registry for Enterprise
TypeScript
608
star
4

npminstall

Make `npm install` fast and easy.
JavaScript
497
star
5

binary-mirror-config

Binary mirror config for prebuild and node-pre-gyp
JavaScript
162
star
6

cnpmweb

Web for cnpmcore
TypeScript
90
star
7

bug-versions

collect all bug versions on npm package.
JavaScript
83
star
8

nodeinstall

Another node installer that bundle node with application.
JavaScript
83
star
9

mirrors

‼️ ‼️ ‼️ ‼️ DEPRECATED, please use https://github.com/cnpm/cnpmcore ‼️ ‼️ ‼️ ‼️
JavaScript
73
star
10

rapid

The fastest way to install npm packages.
Rust
38
star
11

koa-limit

koa middleware for limiting requests by ip
JavaScript
31
star
12

unpkg-white-list

npmmirror.com 允许开启 unpkg 功能的白名单列表,避免被当作网盘滥用
JavaScript
30
star
13

web.cnpmjs.org

Web app of registry.cnpmjs.org
JavaScript
14
star
14

oss-cnpm

oss wraper for https://cnpmjs.org
JavaScript
11
star
15

upyun-storage

Upyun wrapper for cnpm
JavaScript
9
star
16

sfs

Simple file store
JavaScript
7
star
17

rapid.app

The desktop app for rapid npm installation.
JavaScript
5
star
18

fs-cnpm

fs storage wrapper for cnpm
JavaScript
4
star
19

errors

cnpm errors
TypeScript
4
star
20

safe-publish-tag

Get the safe publish tag when you `npm publish`
JavaScript
3
star
21

npminstall-ci

CI Tests for [npminstall](https://npmjs.com/npminstall)
Shell
2
star
22

r.cnpmjs.org

r.cnpmjs.org base on cnpmcore
TypeScript
2
star
23

tfs-cnpm

a tfs api wraper for cnpm
JavaScript
1
star
24

upyun-cnpm

co-upyun-storage for cnpm
JavaScript
1
star
25

npminstall-benchmark

npminstall vs npm vs pnpm vs yarn
Makefile
1
star
26

co-upyun-storage

Upyun-storage for co
JavaScript
1
star
27

cos-cnpm

tencent-cloud cos wrapper for cnpm
JavaScript
1
star
28

qn-cnpm

qn wrapper for cnpm
JavaScript
1
star