Koa - A successor for Express

Refs

  1. Koa中文文档

Why Koa ?

Smaller, more expressive, more robust.

  • VS Epxress
    The key difference between Koa and Express is how they handle middleware. Express includes routing and templates in the application framework. Koa, on the other hand, requires modules for these features, therefore making it more modular or customizable.

  • Koa application
    A Koa application is an object containing an array of middleware functions which are composed and executed in a stack-like manner upon request.

  • Cascading

Use Koa

Hello world

1
2
3
4
5
6
7
8
9
const Koa = require('koa');
const app = new Koa();

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

app.listen(3000);
// now, any request from localhost:3000 will return Hello world

Middlewares

Middlewares can use aysnc/await to control execution flow.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const Koa = require('koa');
const app = new Koa();

// logger

app.use(async (ctx, next) => {
await next();
const rt = ctx.response.get('X-Response-Time');
console.log(`${ctx.method} ${ctx.url} - ${rt}`);
});

// x-response-time

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

// response

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

app.listen(3000);

One Application for both HTTP and https

1
2
3
4
5
6
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);
Author

Chendongtian

Posted on

2022-07-22

Updated on

2023-08-04

Licensed under

Comments