# 上下文
koa的中間件
```javascript
app.use(function *(next){
this; // is the Context
this.request; // is a koa Request
this.response; // is a koa Response
});
```
說明:
- this是上下文(注釋1*)
- *代表es6里的generator
http模型里的請求和響應
- this.request
- this.response
對比express的中間件
```javascript
app.use(function (req, res, next) {
return next();
});
```
express里的req和res是顯式聲明,看起來更清晰一些
next處理是一樣的,二者無差異
注釋1: 此處的this 并不同于通常狀態下的this 指向(即調用者)。在koa中 this 指向每一次的請求,在請求接受后初始化,在一次請求結束后被釋放。
- Introduction
- Nodejs 4.x新特性
- classes
- typed arrays
- generators
- collections
- Set
- Map
- arrow functions
- block scoping
- template strings
- promises
- symbols
- Koa基礎
- 上下文
- koa-generator
- 安裝
- 創建項目
- 更改視圖模板引擎
- Routes
- HTTP
- Get
- 如何獲取query參數
- 如何獲取params
- Post
- 從post獲取參數
- 標準表單(Post with x-www-form-urlencoded)
- 文件上傳(Post with form-data)
- Post with raw
- 數據庫
- MySQL
- Mongo
- 流程控制
- generator/co
- es6的generator是什么?
- co = generator + promise
- async/await
- promise with bluebird
- 測試
- Mocha
- Supertest
- 部署
- 最佳實踐
- FAQ
- 如何發布本書到git pages
- 如何知道require模塊的用法
- koa中的異常處理