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

venables/koa-plus: The Koa framework extended for APIs. Optimized for security, ...

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

开源软件名称:

venables/koa-plus

开源软件地址:

https://github.com/venables/koa-plus

开源编程语言:

JavaScript 100.0%

开源软件介绍:

koa-plus

Version Build Status Coverage Status Dependency Status Standard - JavaScript Style Guide Downloads

koa-plus is the koa framework (v2) extended for APIs. Optimized for security, scalability, and productivity.

Features

  • Important security headers via helmet.
  • CORS support via kcors.
  • Adds an X-Response-Time header to all responses.
  • Adds an X-Request-Id header to all requests as they come in for easier debugging.
    • Also passes through client/proxy/load-balancer generated X-Request-Id headers as X-Client-Request-Id
  • Uses koa-body to parse any request body type
  • Adds ETag headers to allow conditional GET requests (respond with 304 Not Modified)
  • Object stream support via koa-json
  • Request logging via koa-morgan
  • Simple ctx.debug (or ctx.app.debug) logging via the debug module
  • Pretty-printed JSON in development
  • Exposes the app configuration on ctx as ctx.config (or, app.context.config)

Each feature can be disabled individually.

Installation

Install koa-plus via yarn or npm:

yarn add koa-plus
npm install koa-plus --save

Usage

Existing apps:

Simply replace your existing koa require with koa-plus

Old:

const Koa = require('koa')
const app = new Koa()
// ...

New:

const Koa = require('koa-plus')
const app = new Koa()
// ...

Configuration

Some of the middleware included in koa-plus allows for options. To pass options to these middleware, simply pass the options to the constructor.

Options

  • body: Use the same options as the koa-body middleware accepts. Docs
  • compress: Use the same options as the koa-compress middleware accepts. Docs
  • cors: Use the same options as the kcors middleware accepts. Docs
  • debug: Set the name of the debug logger
  • helmet: Use the same options as the helmet middleware accepts. Docs
  • json: Use the same options as the koa-json middleware accepts. Docs
  • logger: Use format for the logger format, and the remaining options as what morgan accepts Docs

Example

const Koa = require('koa-plus')

const app = new Koa({
  body: {
    jsonLimit: '10kb' // Sets the json request body limit to 10k
  },
  compress: {
    threshold: 2048 // Sets the threshold to Gzip responses at 2k (2048 bytes)
  },
  cors: {
    origin: '*' // Set the `Access-Control-Allow-Origin` header to be `*`
  },
  debug: {
    name: 'worker' // Set the debug logger name
  },
  helmet: {
    noCache: true,  // Sets the `Cache-Control` headers to prevent caching
    frameguard: {
      action: 'deny' // Set the `X-Frame-Options' header to be `DENY`
    }
  },
  json: {
    pretty: false // Disables pretty-printing
  },
  logger: {
    format: 'dev' // Use the `dev` format of logging
  }
})

Disabling middleware

Each of the middleware in koa-plus can be disabled individually by using the enabled option.

As an example, to reset koa-plus back to basic koa functionality, use the following config:

const Koa = require('koa-plus')

const app = new Koa({
  body: {
    enabled: false
  },
  compress: {
    enabled: false
  },
  cors: {
    enabled: false
  },
  debug: {
    enabled: false
  },
  etag: {
    enabled: false
  },
  helmet: {
    enabled: false
  },
  json: {
    enabled: false
  },
  logger: {
    enabled: false
  },
  requestId: {
    enabled: false
  },
  responseTime: {
    enabled: false
  }
})

Testing

To run the tests locally, simply run

yarn test

or

npm test



鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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