@FarmerZ
2018-03-11T06:04:38.000000Z
字数 4652
阅读 750
koa
次世代nodejs 的 web框架
koa是由Express幕后团队打造的,目的是更小,更快,更稳定的web应用和apis。通过杠杆生成器(leveraging generators)Koa可以让你引导(ditch)回调函数,极大的提升错误处理。Koa核心不集成任何的中间件,其本身提供的优雅的功能套件就能够写出既快又nice的服务器。
一个Koa环境(实例)封装了node原生的请求和返回对象到一个单独的对象中,这个单独的对象提供了许多使用的方法,能够编写web应用和API。这些HTTP服务器开发中经常用到的操作被添加到当前等级,而不是高等级。他将强制中间件重新实现这些常用的功能。
一个环境Context
在每次请求时被创建,并且被引用至中间件作为接收器,或者定义成this
。如下所示。
app.use(function *(){
this;//这里是koa环境context
this.request;//是一个koa请求
this.response;//是一个koa返回
})
很多context
环境访问器和方法只是ctx.request
koa请求或者ctx.response
koa返回的代理,主要是为了方便。例如ctx.type
和ctx.length
代表response
返回对象,ctx.paht
和ctx.methos
代表请求。
API 接口。
环境(Context)定义的方法和访问器。
request
对象。response
对象。 request
对象。response
对象。options
获得cookie
名字。 options
设置name
的值value
。 /
。true
。 options
来使用cookie模块。.status
的值为500时抛出,koa在返回的信息中适当处理。限免的组合也是可疑的。 例如:this.throw('name require', 400)
等于
var err = new Error('name require');
err.status = 400;
throw err;
注意这些是用户自定义的错误,使用err.expose
发出。因此只适合某些客户端的反馈。这些错误不同于内置的错误信息提醒,因为错误的详细信息不会泄露。
你也许传递一个properties
选项对象,他和原来的错误信息进行了整合,对于人性化体验很有帮助,它报告个给请求者一个回溯流(upsteam
)。
this.throw(401,'access_denied',{user:user});
this.throw('access_denied',{user:user});
koa使用http-errors
来创建错误。
.throw()
,当!value
是类似node的assert()
方法。
this.assert(this.sate.user,401,'User not found, Please login!');
koa使用http-assert
实现断言(assertions
)
this.response = false;
如果你想使用原生的res对象处理而不是koa的response处理,那么就使用它。注意那种用法koa不支持。这也许会打断koa中间件本来的功能,或者koa也被打断。使用这个属性最好考虑一下hack,这是使用传统的fn(req,res)
方法和koa中间件的唯一方便的方法。
请求别名Request aliases
下面的访问起和Request别名相等。
- ctx.header
- ctx.headers
- ctx.method
- ctx.method=
- ctx.url
- ctx.url=
- ctx.originalUrl
- ctx.origin
- ctx.href
- 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()
返回别名Response aliases
下面的访问起和返回别名相等
- ctx.body
- ctx.body=
- ctx.status
- ctx.status=
- ctx.message
- ctx.message=
- ctx.length=
- ctx.length
- ctx.type
- ctx.type=
- ctx.handerSent
- ctx.redirect()
- ctx.attachment()
- ctx.set()
- ctx.append()
- ctx.remove()
- ctx.lastModified=
- ctx.etag=
一个koa请求Request对象是个建立在node请求request之上的抽象。提供了一些额外的功能,这对每个http服务器开发者来说非常有用。
request.header
。methodoverride()
。
this.request.origin
//=>http://example.com
this.request.href
//=>http://example.com/foo/bar?q=1
?
。?
。X-Forwarded-Host
当app.proxy
是true,否则是常用的host
。X-Frowarded-Host
当app.proxy
是true,否则是常用的。Content-type
,不包括一些参数,如charset
。
var ct = this.request.type.
//=>'image/png'
undefined
。
this.request.charset
//=>'utf-8'
color=blue&size=small
。
{
color:'blue',
size:'small'
}
this.query = {next:'/login'};
if-None-Match
和if-Modified-Since
以及last-modified
之间的缓存沟通。他必须能够引用到更改之后的返回头部response headers
//freshness check requeire stats 20x or 304
this.status = 200;
this.set('ETag','123');
//cache is ok
if(this.fresh) {
this.status = 304;
return;
}
//cache is stale
//fetch new data
shis.body = yield db.find('something');
request.fresh
相反https
或者http
。支持X-Forwarded-Proto
当app.proxy
是true。this.protocol == "https"
的速记,用以检查一个求情能否通过安全传输层。X-Forwarded-For
当app.proxy
为true。X-Forwarded-For
并且app.proxy
可用,那么返回这些的ip的一个数组。