koa.js

koa的context

ctx.request和ctx.response是koa本身封装的,ctx.res, ctx,req是原生自带的,例如 ctx.type 和 ctx.length 委托给 response 对象,ctx.path 和 ctx.method 委托给 request。express

ctx.request; // 这是 koa Request
ctx.response; // 这是 koa Response
ctrx.req //原始的http请求对象
ctx.res //原始的http响应对象
ctx.app // 应用程序实例引用
ctx.request // Koa2中context通过封装的请求对象
复制代码

错误处理

  1. 所以发生错误时能够app.emit('error', err, ctx)
app.on('error', (err, ctx) => {
  log.error('server error', err, ctx)
});
复制代码
  1. ctx.throw([status], [msg], [properties])
ctx.throw(400, 'name required')
===
const err = new Error('name required');
err.status = 400;
err.expose = true;
throw err;
复制代码

request

ctx.header
ctx.headers
ctx.method
ctx.method=
ctx.url
ctx.url=
ctx.originalUrl
ctx.origin
ctx.href
ctx.path
ctx.path=
ctx.query
ctx.query=
ctx.querystring
ctx.querystring=
ctx.host
ctx.hostname
ctx.fresh
ctx.stale
ctx.socket
ctx.protocol
ctx.secure
ctx.ip
ctx.ips
ctx.subdomains
ctx.is()
ctx.accepts()
ctx.acceptsEncodings()
ctx.acceptsCharsets()
ctx.acceptsLanguages()
ctx.get() // 拿请求头ctx.get('accept-encoding')
复制代码

response

ctx.body
ctx.body=
ctx.status
ctx.status=
ctx.message
ctx.message=
ctx.length=
ctx.length
ctx.type=
ctx.type
ctx.headerSent
ctx.redirect()
ctx.attachment()
ctx.set() //设置请求头
ctx.append()
ctx.remove()
ctx.lastModified=
ctx.etag=
复制代码

koa vs expressbash

// express        koa
// express.static koa-static
// express        koa
// bodyParser  => koa-bodyPaser
// x              koa-view 
// cookie-parser  x
// express-session koa-session
// multer         koa-better-body
// x              koa-router
复制代码
相关文章
相关标签/搜索