第四代Express框架koa简介

简介

熟悉Spring MVC的朋友应该都清楚Spring MVC是基于servlet的代码框架,这是最传统的web框架。而后在Spring5中引入了Spring WebFlux,这是基于reactive-netty的异步IO框架。node

一样的,nodejs在最初的Express 3基础上发展起来了异步的koa框架。koa使用了promises和aysnc来避免JS中的回调地狱,而且简化了错误处理。react

今天咱们要来介绍一下这个优秀的nodejs框架koa。web

koa和express

koa再也不使用nodejs的req和res,而是封装了本身的ctx.request和ctx.response。spring

express能够看作是nodejs的一个应用框架,而koa则能够当作是nodejs 的http模块的抽象。express

和express提供了Middleware,Routing,Templating,Sending Files和JSONP等特性不一样的是,koa的功能很单一,若是你想使用其余的一些功能好比routing,sending files等功能,可使用koa的第三方中间件。promise

koa并非来替换express的,就像spring webFlux并非用来替换spring MVC的。koa只是用Promises改写了控制流,而且避免了回调地狱,并提供了更好的异常处理机制。cookie

koa使用介绍

koa须要node v7.6.0+版原本支持ES2015和async function。app

咱们看一个最最简单的koa应用:框架

const Koa = require('koa');
const app = module.exports = new Koa();

app.use(async function(ctx) {
  ctx.body = 'Hello World';
});

if (!module.parent) app.listen(3000);

koa应用程序就是一个包含了不少个中间件的对象,这些中间件将会按照相似stack的执行顺序一个相应request。dom

中间件的级联关系

koa.use中传入的是一个function,咱们也能够称之为中间件。

koa能够use不少个中间件,举个例子:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  await next();
  console.log('log3');
});

app.use(async (ctx, next) => {
  await next();
  console.log('log2');
});

app.use(async ctx => {
  console.log('log3');
});

app.listen(3000);

上面的例子中,咱们调用了屡次next,只要咱们调用next,调用链就会传递到下一个中间件进行处理,一直到某个中间件再也不调用next
为止。

上面的代码运行输出:

log1
log2
log3

koa的构造函数

咱们看下koa的构造函数:

constructor(options) {
    super();
    options = options || {};
    this.proxy = options.proxy || false;
    this.subdomainOffset = options.subdomainOffset || 2;
    this.proxyIpHeader = options.proxyIpHeader || 'X-Forwarded-For';
    this.maxIpsCount = options.maxIpsCount || 0;
    this.env = options.env || process.env.NODE_ENV || 'development';
    if (options.keys) this.keys = options.keys;
    this.middleware = [];
    this.context = Object.create(context);
    this.request = Object.create(request);
    this.response = Object.create(response);
    // util.inspect.custom support for node 6+
    /* istanbul ignore else */
    if (util.inspect.custom) {
      this[util.inspect.custom] = this.inspect;
    }
  }

能够看到koa接收下面几个参数:

  • app.env 默认值是NODE_ENV或者development
  • app.keys 为cookie签名的keys

看下怎么使用:

app.keys = ['secret1', 'secret2'];
app.keys = new KeyGrip(['secret1', 'secret2'], 'sha256');

ctx.cookies.set('name', 'jack', { signed: true });
  • app.proxy 是否支持代理
  • app.subdomainOffset 表示子域名是从第几级开始的,这个参数决定了request.subdomains的返回结果,默认值为2
  • app.proxyIpHeader proxy ip header默认值是X-Forwarded-For
  • app.maxIpsCount 从proxy ip header读取的最大ip个数,默认值是0表示无限制。

咱们能够这样用:

const Koa = require('koa');
const app = new Koa({ proxy: true });

或者这样用:

const Koa = require('koa');
const app = new Koa();
app.proxy = true;

启动http server

koa是一种web框架,web框架就须要开启http服务,要启动http服务,须要调用nodejs中的Server#listen()方法。

在koa中,咱们能够很方便的使用koa#listen方法来启动这个http server:

const Koa = require('koa');
const app = new Koa();
app.listen(3000);

上面的代码至关于:

const http = require('http');
const Koa = require('koa');
const app = new Koa();
http.createServer(app.callback()).listen(3000);

固然你能够同时建立http和https的服务:

const http = require('http');
const https = require('https');
const Koa = require('koa');
const app = new Koa();
http.createServer(app.callback()).listen(3000);
https.createServer(app.callback()).listen(3001);

自定义中间件

koa中的中间件是参数值为(ctx, next)的function。在这些方法中,须要手动调用next()以传递到下一个middleware。

下面看一下自定义的中间件:

async function responseTime(ctx, next) {
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  ctx.set('X-Response-Time', `${ms}ms`);
}

app.use(responseTime);
  • 给中间件起个名字:

虽然中间件function只接收参数(ctx, next),可是我能够将其用一个wrapper方法包装起来,在wrapper方法中,咱们给中间件起个名字 :

function logger(name) {
  return async function logger(ctx, next) {
      console.log(name);
      await next();
  };    
}
  • 自定义中间件的扩展:

上面的wrapper建立方式还有另一个好处,就是能够在自定义中间件中访问传入的参数,从而能够根据传入的参数,对自定义中间件进行扩展。

function logger(format) {
  format = format || ':method ":url"';

  return async function (ctx, next) {
    const str = format
      .replace(':method', ctx.method)
      .replace(':url', ctx.url);

    console.log(str);

    await next();
  };
}

app.use(logger());
app.use(logger(':method :url'));
  • 组合多个中间件:

当有多个中间件的状况下,咱们可使用compose将其合并:

const compose = require('koa-compose');
const Koa = require('koa');
const app = module.exports = new Koa();

// x-response-time

async function responseTime(ctx, next) {
  const start = new Date();
  await next();
  const ms = new Date() - start;
  ctx.set('X-Response-Time', ms + 'ms');
}

// logger

async function logger(ctx, next) {
  const start = new Date();
  await next();
  const ms = new Date() - start;
  if ('test' != process.env.NODE_ENV) {
    console.log('%s %s - %s', ctx.method, ctx.url, ms);
  }
}

// response

async function respond(ctx, next) {
  await next();
  if ('/' != ctx.url) return;
  ctx.body = 'Hello World';
}

// composed middleware

const all = compose([
  responseTime,
  logger,
  respond
]);

app.use(all);

if (!module.parent) app.listen(3000);

异常处理

在koa中怎么进行异常处理呢?

通用的方法就是try catch:

app.use(async (ctx, next) => {
  try {
    await next();
  } catch (err) {
    err.status = err.statusCode || err.status || 500;
    throw err;
  }
});

固然你也能够自定义默认的error处理器:

app.on('error', err => {
  log.error('server error', err)
});

咱们还能够传入上下文信息:

app.on('error', (err, ctx) => {
  log.error('server error', err, ctx)
});
本文做者:flydean程序那些事

本文连接:http://www.flydean.com/koa-startup/

本文来源:flydean的博客

欢迎关注个人公众号:「程序那些事」最通俗的解读,最深入的干货,最简洁的教程,众多你不知道的小技巧等你来发现!

相关文章
相关标签/搜索