Koa

Expressive, light-weight HTTP framework for node.js to make web applications and APIs more enjoyable to write.

Koa makes it easy to write REST APIs with support for the previously discussed ES2016 features. It was written by one of the core contributors of Express, the widely spread HTTP framework for Node.js. They are very similar, but compared to Koa it is pretty dated. Here is a very minimal example of a Koa web server:


import Koa from 'koa'
const app = new Koa()

app.use(ctx => {
  ctx.body = 'Hello!'
})

app.listen(3000)


// $ curl localhost:3000
// Hello!

Since one of the main goals of Koa is to be light-weight and minimal, it doesn't pack too much into its core. Routing is mandatory for most applications, so this is where koa-roater comes in. It's a routing middleware for Koa and looks something like this:

import Koa from 'koa'
import Router from 'koa-router'

const app = new Koa()
const router = new Router()

router.get('/', ctx => {
  ctx.body = 'Hello!'
})

router.get('/:name', ctx => {
 ctx.body = `Hello, ${ctx.params.name}!`
})

app.use(router.routes())
app.listen(3000)

// $ curl localhost:3000
// Hello!

// $ curl localhost:3000/Emanuel
// Hello, Emanuel!

results matching ""

    No results matching ""