koa.js

koa的context

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

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 express

// 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
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值