@FarmerZ
2018-03-11T06:04:38.000000Z
字数 4652
阅读 789
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环境contextthis.request;//是一个koa请求this.response;//是一个koa返回})
很多context环境访问器和方法只是ctx.requestkoa请求或者ctx.responsekoa返回的代理,主要是为了方便。例如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 304this.status = 200;this.set('ETag','123');//cache is okif(this.fresh) {this.status = 304;return;}//cache is stale//fetch new datashis.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的一个数组。