kmid
v1.6.1
Published
koa2 中间件集合
Downloads
43
Readme
kmid
koa2 中间件集
access(options)
请求记录
options
配置warnTime
number [请求时间超过 该数值 显示警告。 单位 ms]filters
array [过滤 path 不显示请求记录]
app.use(kmid.access());
queryfix()
处理微信商城 url 后被加上?10000skip=true
时导致query
参数解析错乱的问题
app.use(kmid.queryfix());
parse(opts)
body 数据解析中间件
cobody
See co-body for a full list of optionslimit
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 optionsbytesExpected
--{Integer}-- The expected number of bytes in this form, defaultnull
maxFields
--{Integer}-- Limits the number of fields that the querystring parser will decode, default1000
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, default2mb (2 - 2 - 1024)
uploadDir
--{String}-- Sets the directory for placing file uploads in, defaultos.tmpDir()
keepExtensions
--{Boolean}-- Files written touploadDir
will include the extensions of the original files, defaultfalse
hash
--{String}-- If you want checksums calculated for incoming files, set this to either'sha1'
or'md5'
, defaultfalse
multiples
--{Boolean}-- Multiple file uploads or no, defaulttrue
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
app.use(kmid.parse());
proxy(prefix, opts)
请求转发中间件 依赖http-proxy
prefix
匹配前缀下的所有请求都会转发opts
配置 更多参数请见http-proxyrewrite
重写 url 函数target
转发 urlwarnTime
显示警告 时间阀
// middleware
app.use(
kmid.proxy('/services', {
target: 'https://api.github.com/users',
changeOrigin: true,
rewrite: path => path.replace(/^\/services(\/|\/\w+)?$/, ''),
warnTime: 10 * 1000
})
);
static(root, opts)
静态文件 依赖koa-send
root
根目录. nothing above this root directory can be servedopts
配置defer
是否滞后处理 (defaults tofalse
)maxage
浏览器缓存 max-age 毫秒. (defaults to0
)hidden
是否响应隐藏文件 (defaults tofalse
)gzip
开启 gzip (defaults totrue
)format
If notfalse
(defaults totrue
), 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 tofalse
)
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) 控制输出是否被转义,查看 AutoescapingthrowOnUndefined
(默认值: 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 配置 更多配置见ioredisexpire
session 过期时间 millisecondsport
Redis porthost
Redis hostpassword
redis password,db
redis db
cookie
cookies.set optionsmaxAge
a number representing the milliseconds from Date.now() for expiryexpires
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.
app.use(kmid.session({
store: {
expire: 86400000*7
port: config.redis.port,
host: config.redis.host,
password: config.redis.password,
db: config.redis.sessionDB
},
maxAge: 86400000*7,
key: 'web_wx',
}));
//
ctx.session.username = 'jaskang'
//
const user = ctx.session.username;
中间件来源
koa-body
https://github.com/dlau/koa-body/tree/koa2koa-static
https://github.com/koajs/statickoa-view
https://github.com/d-band/koa-viewkoa-proxies
https://github.com/vagusX/koa-proxies