2.2.0 • Published 4 months ago
@mutoe/koam-core v2.2.0
Koam
Implement a simple Koa-like http server with zero dependencies from scratch.
THIS FRAMEWORK HAVE NOT BEEN STRICTLY TESTED, PLEASE DO NOT USE IT IN PRODUCTION ! 许多功能未经严格测试,请勿用于生产目的!
Advantage
- Lightweight (0 dependency)
- TypeScript friendly
- Built-in body parser middleware
- Built-in response time middleware
Notes
- Current only support
application/json
type request and response body ctx.assert
must explicit declareContext
type. See microsoft/Typescript#34523app.use(async (ctx: Context, next) => { // ^^^^^^^ const val: unknown = 1.2345 // ^^^^^^^ ctx.assert(typeof val === 'number', 500) console.log(val.toFixed(2)) // ^^^ now val is number type })
Roadmap
Configuration
app.env
app.proxy
see https://github.com/mutoe/koam#3-cookieapp.keys
app.proxyIpHeader
app.maxIpsCount
app.onError(error, ctx)
Application properties / methods
app.use(middleware)
app.callback()
app.listen(...)
app.context
app.keys=
usingapp.on('error', error)
app.onError
insteadapp.address
app.port
Context properties
ctx.req
ctx.res
ctx.request
ctx.response
ctx.state
ctx.app
usingctx.app.emit
ctx.throw
insteadsee https://github.com/mutoe/koam#3-cookiectx.cookies
ctx.throw([status], [message], [detail])
ctx.throw(appError)
ctx.assert(value, [status], [message], [detail])
ctx.respond
Context request
request.headers
ctx.headers
request.header
ctx.header
request.headers=
request.header=
request.get(field)
ctx.get(field)
request.method
ctx.method
request.method=
ctx.method=
request.length
request.protocol
ctx.protocol
request.host
ctx.host
request.hostname
ctx.hostname
request.url
ctx.url
request.url=
ctx.url=
request.originalUrl
ctx.originalUrl
request.origin
ctx.origin
request.URL
request.href
ctx.href
request.path
ctx.path
request.path=
ctx.path=
request.query
ctx.query
request.query=
ctx.query=
request.querystring
ctx.querystring
request.search
request.querystring=
ctx.querystring=
request.search=
request.type
(getmime-type
inContent-Type
header)request.charset
(getcharset
inContent-Type
header)request.fresh
ctx.fresh
request.socket
ctx.socket
request.stale
ctx.stale
request.secure
ctx.secure
request.ip
ctx.ip
request.ips
ctx.ips
request.subdomains
ctx.subdomains
request.is()
ctx.is()
request.accepts()
ctx.accepts()
request.acceptsEncodings()
ctx.acceptsEncodings()
request.acceptsCharsets()
ctx.acceptsCharsets()
request.acceptsLanguages()
ctx.acceptsLanguages()
request.idempotent
Context response
response.headers
response.header
response.headerSent
ctx.headerSent
response.flushHeaders()
response.has(header)
response.get(header)
response.set(headers)
ctx.set(headers)
response.append(header, value)
ctx.append(header, value)
response.remove(header)
ctx.remove(header)
response.socket
response.body
ctx.body
response.body=
ctx.body=
(currently only finished json body and stream)response.status
ctx.status
response.status=
ctx.status=
response.message
ctx.message
response.message=
ctx.message=
response.length
ctx.length
response.length=
ctx.length=
response.type
ctx.type
response.type=
ctx.type=
response.is(mimeTypes...)
response.redirect(url, [alt])
ctx.redirect(url, [alt])
response.attachment([filename])
ctx.attachment([filename])
response.lastModified=
ctx.lastModified=
response.etag=
ctx.etag=
response.vary(field)