• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

cnpm/koa-middlewares: easy way to use some useful koa middlewares

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

cnpm/koa-middlewares

开源软件地址:

https://github.com/cnpm/koa-middlewares

开源编程语言:

JavaScript 100.0%

开源软件介绍:

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




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
soraping/koa-ts: koa2+typescript发布时间:2022-06-24
下一篇:
RisingStack/koa-prerender: KOA middleware for prerendering javascript-rendered p ...发布时间:2022-06-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap