1. 导入koa :导入koa,和koa 1.x不同,在koa2中,我们导入的是一个class,因此用大写的Koa表示:

      1. const Koa = require('koa');
    2. 创建一个Koa对象表示web app本身:

      1. const app = new Koa();
    3. 对于任何请求,app将调用该异步函数处理请求:

      1. app.use(async (ctx, next) => {
      2. await next();
      3. ctx.response.type = 'text/html';
      4. ctx.response.body = '<h1>Hello, koa2!</h1>';
      5. });
    4. 在端口3000监听:

      1. app.listen(3000);
      2. console.log('app started at port 3000...');
    5. 对于每一个http请求,koa将调用我们传入的异步函数来处理:

      1. async (ctx, next) => {
      2. await next();
      3. // 设置response的Content-Type:
      4. ctx.response.type = 'text/html';
      5. // 设置response的内容:
      6. ctx.response.body = '<h1>Hello, koa2!</h1>';
      7. }

      其中,参数ctx是由koa传入的封装了request和response的变量,我们可以通过它访问request和response,next是koa传入的将要处理的下一个异步函数。
      上面的异步函数中,我们首先用await next();处理下一个异步函数,然后,设置response的Content-Type和内容。
      由async标记的函数称为异步函数,在异步函数中,可以用await调用另一个异步函数,这两个关键字将在ES7中引入。

    6. 导入koa

      1. npm install koa@2.0.0
    7. koa middleware

      1. app.use(async (ctx, next) => {
      2. await next();
      3. ctx.response.type = 'text/html';
      4. ctx.response.body = '<h1>Hello, koa2!</h1>';
      5. });

      每收到一个http请求,koa就会调用通过app.use()注册的async函数,并传入ctx和next参数。
      我们可以对ctx操作,并设置返回内容。但是为什么要调用await next()?
      原因是koa把很多async函数组成一个处理链,每个async函数都可以做一些自己的事情,然后用await next()来调用下一个async函数。我们把每个async函数称为middleware,这些middleware可以组合起来,完成很多有用的功能

    8. 实例 ``javascript app.use(async (ctx, next) => { console.log(${ctx.request.method} ${ctx.request.url}`); // 打印URL await next(); // 调用下一个middleware });

    app.use(async (ctx, next) => { const start = new Date().getTime(); // 当前时间 await next(); // 调用下一个middleware const ms = new Date().getTime() - start; // 耗费时间 console.log(Time: ${ms}ms); // 打印耗费时间 });

    app.use(async (ctx, next) => { await next(); ctx.response.type = ‘text/html’; ctx.response.body = ‘

    Hello, koa2!

    ‘; });

    1. middleware的顺序很重要,也就是调用app.use()的顺序决定了middleware的顺序。<br />此外,如果一个middleware没有调用await next(),会怎么办?答案是后续的middleware将不再执行了。这种情况也很常见,例如,一个检测用户权限的middleware可以决定是否继续处理请求,还是直接返回403错误:
    2. ```javascript
    3. app.use(async (ctx, next) => {
    4. if (await checkUserPermission(ctx)) {
    5. await next();
    6. } else {
    7. ctx.response.status = 403;
    8. }
    9. });