2.4.0 • Published 1 year ago
@mutoe/koam-core v2.4.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/jsontype request and response body ctx.assertmust explicit declareContexttype. 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.envapp.proxysee https://github.com/mutoe/koam#3-cookieapp.keysapp.proxyIpHeaderapp.maxIpsCountapp.onError(error, ctx)
Application properties / methods
app.use(middleware)app.callback()app.listen(...)app.contextapp.keys=usingapp.on('error', error)app.onErrorinsteadapp.addressapp.port
Context properties
ctx.reqctx.resctx.requestctx.responsectx.statectx.appusingctx.app.emitctx.throwinsteadsee https://github.com/mutoe/koam#3-cookiectx.cookiesctx.throw([status], [message], [detail])ctx.throw(appError)ctx.assert(value, [status], [message], [detail])ctx.respond
Context request
request.headersctx.headersrequest.headerctx.header
request.headers=request.header=
request.get(field)ctx.get(field)request.methodctx.methodrequest.method=ctx.method=request.lengthrequest.protocolctx.protocolrequest.hostctx.hostrequest.hostnamectx.hostnamerequest.urlctx.urlrequest.url=ctx.url=request.originalUrlctx.originalUrlrequest.originctx.originrequest.URLrequest.hrefctx.hrefrequest.pathctx.pathrequest.path=ctx.path=request.queryctx.queryrequest.query=ctx.query=request.querystringctx.querystringrequest.search
request.querystring=ctx.querystring=request.search=
request.type(getmime-typeinContent-Typeheader)request.charset(getcharsetinContent-Typeheader)request.freshctx.freshrequest.socketctx.socketrequest.stalectx.stalerequest.securectx.securerequest.ipctx.iprequest.ipsctx.ipsrequest.subdomainsctx.subdomainsrequest.is()ctx.is()request.accepts()ctx.accepts()request.acceptsEncodings()ctx.acceptsEncodings()request.acceptsCharsets()ctx.acceptsCharsets()request.acceptsLanguages()ctx.acceptsLanguages()request.idempotent
Context response
response.headersresponse.header
response.headerSentctx.headerSentresponse.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.socketresponse.bodyctx.bodyresponse.body=ctx.body=(currently only finished json body and stream)response.statusctx.statusresponse.status=ctx.status=response.messagectx.messageresponse.message=ctx.message=response.lengthctx.lengthresponse.length=ctx.length=response.typectx.typeresponse.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(fields)ctx.vary(fields)