Napping Panda Missionaries


    1.6.1 • Public • Published


    npm npm

    koa2 中间件集

    access(options) 请求记录

    • options 配置
      • warnTime number [请求时间超过 该数值 显示警告。 单位 ms]
      • filters array [过滤 path 不显示请求记录]

    queryfix() 处理微信商城 url 后被加上?10000skip=true时导致query参数解析错乱的问题


    parse(opts) body 数据解析中间件

    • cobody See co-body for a full list of options
      • limit number or string representing the request size limit (10mb)
      • strict when set to true, JSON parser will only accept arrays and objects; when false will accept anything JSON.parse accepts. Defaults to true. (also strict mode will always return object).
      • queryString an object of options when parsing query strings and form data. See qs for more information.
      • jsonTypes is used to determine what media type co-body will parse as json, this option is passed directly to the type-is library.
      • formTypes is used to determine what media type co-body will parse as form, this option is passed directly to the type-is library.
      • textTypes is used to determine what media type co-body will parse as text, this option is passed directly to the type-is library.
    • formidable See node-formidable for a full list of options
      • bytesExpected --{Integer}-- The expected number of bytes in this form, default null
      • maxFields --{Integer}-- Limits the number of fields that the querystring parser will decode, default 1000
      • maxFieldsSize --{Integer}-- Limits the amount of memory all fields together (except files) can allocate in bytes. If this value is exceeded, an 'error' event is emitted, default 2mb (2 - 2 - 1024)
      • uploadDir --{String}-- Sets the directory for placing file uploads in, default os.tmpDir()
      • keepExtensions --{Boolean}-- Files written to uploadDir will include the extensions of the original files, default false
      • hash --{String}-- If you want checksums calculated for incoming files, set this to either 'sha1' or 'md5', default false
      • multiples --{Boolean}-- Multiple file uploads or no, default true
      • onFileBegin --{Function}-- Special callback on file begin. The function is executed directly by formidable. It can be used to rename files before saving them to disk. See the docs
    • onerror

    proxy(prefix, opts) 请求转发中间件 依赖http-proxy

    • prefix 匹配前缀下的所有请求都会转发
    • opts 配置 更多参数请见http-proxy
      • rewrite 重写 url 函数
      • target 转发 url
      • warnTime 显示警告 时间阀
    // middleware
      kmid.proxy('/services', {
        target: '',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/services(\/|\/\w+)?$/, ''),
        warnTime: 10 * 1000

    static(root, opts) 静态文件 依赖koa-send

    • root 根目录. nothing above this root directory can be served
    • opts 配置
      • defer 是否滞后处理 (defaults to false)
      • maxage 浏览器缓存 max-age 毫秒. (defaults to 0)
      • hidden 是否响应隐藏文件 (defaults to false)
      • gzip 开启 gzip (defaults to true)
      • format If not false (defaults to true), format the path to serve static file servers and not require a trailing slash for directories, so that you can do both /directory and /directory/
      • setHeaders Function to set custom headers on response.
      • extensions 匹配后缀 数组 (defaults to false)
    app.use(kmid.static(path.join(__dirname, '..', 'static'), opts));

    views(root, opts) 视图中间件 依赖nunjucks 模板

    • root: 指定存放模板的目录 (默认 './views')
    • opts: nunjucks 模板引擎的设置 nunjucks 文档
      • ext: (默认值 html) 模板文件后缀
      • filters: 过滤器 [{name:func},{name:func}]
      • globals: 全局变量 [{name:value},{name:value}]
      • autoescape (默认值: true) 控制输出是否被转义,查看 Autoescaping
      • throwOnUndefined (默认值: false) 当输出为 null 或 undefined 会抛出异常
      • trimBlocks (默认值: false) 自动去除 block/tag 后面的换行符
      • lstripBlocks (默认值: false) 自动去除 block/tag 签名的空格
      • watch (默认值: false) 当模板变化时重新加载。使用前请确保已安装可选依赖 chokidar。
      • noCache (默认值: false) 不使用缓存,每次都重新编译
      • tags (默认值: see nunjucks syntax) 定义模板语法,查看 Customizing Syntax
    app.use(kmid.views(path.join(__dirname, '..', 'views'), opts));
    await ctx.render('user.html')
    // 或者
    await ctx.render('user')
    // 或者
    yield ctx.render('user')

    session(opts) session 中间件 依赖ioredis

    • key cookie 存储的标识
    • store redis 配置 更多配置见ioredis
      • expire session 过期时间 milliseconds
      • port Redis port
      • host Redis host
      • password redis password,
      • db redis db
    • cookie cookies.set options
      • maxAge a number representing the milliseconds from for expiry
      • expires a Date object indicating the cookie's expiration date (expires at the end of session by default).
      • path a string indicating the path of the cookie (/ by default).
      • domain a string indicating the domain of the cookie (no default).
      • secure a boolean indicating whether the cookie is only to be sent over HTTPS (false by default for HTTP, true by default for HTTPS). Read more about this option below.
      • httpOnly a boolean indicating whether the cookie is only to be sent over HTTP(S), and not made available to client JavaScript (true by default).
      • sameSite a boolean or string indicating whether the cookie is a "same site" cookie (false by default). This can be set to 'strict', 'lax', or true (which maps to 'strict').
      • signed a boolean indicating whether the cookie is to be signed (false by default). If this is true, another cookie of the same name with the .sig suffix appended will also be sent, with a 27-byte url-safe base64 SHA1 value representing the hash of cookie-name=cookie-value against the first Keygrip key. This signature key is used to detect tampering the next time a cookie is received.
      • overwrite a boolean indicating whether to overwrite previously set cookies of the same name (false by default). If this is true, all cookies set during the same request with the same name (regardless of path or domain) are filtered out of the Set-Cookie header when setting this cookie.
        store: {
            expire: 86400000*7
            port: config.redis.port,
            password: config.redis.password,
            db: config.redis.sessionDB
        maxAge: 86400000*7,
        key: 'web_wx',
    ctx.session.username = 'jaskang'
    const user = ctx.session.username;



    npm i kmid

    DownloadsWeekly Downloads






    Unpacked Size

    25.5 kB

    Total Files


    Last publish


    • jaskang