Ask all your coding questions @ codequery.io

Koa is a more lightweight version of Express. It's a middleware framework without all of the extra modules (for routing and templating) found with Express.

Koa was developed by the same team as Express. In fact, many would argue Koa to be "Express 5.0" since it's development was largely the result of some ownership issues with the original Express (see Reddit).

Koa leverages async/await for cleaner code. While Koa originally helped avoid "callback hell", the differences between Koa and Express syntax are more subtle since the release of Node 7...

Basic Routing with Express

const express = require('express') const app = express() const asyncActivity = async () => { return {success:true} } const asyncWrapper = fn => (...args) => fn(...args).catch(args[2]); app.get("/", asyncWrapper(async (req, res, next) => { const result = await asyncActivity(); res.json(result); })); app.use(function errorMiddleware(error, req, res, next) { res.status(500); res.send(error); }); app.listen(3000)

Basic Routing with Koa

const Koa = require('koa'); const Router = require("koa-router"); const app = new Koa(); const router = new Router(); const asyncActivity = async () => { return {success:true} } app.use(async function handleError(context, next) { // call our next middleware try { await next(); // catch any error that might have occurred } catch (error) { context.status = 500; context.body = error; } }); app.use(router.routes()); app.use(router.allowedMethods()); router .get("/", async (context, next) => { const result = await asyncActivity(); context.body = result }); app.listen(3000);

From these examples, you'll notice you save a bit of boilerplate code by using Koa. Specifically you don't have to write an asyncWrapper() function with Koa.

Unlike Express, Koa doesn't include a router module out of the box. You would have to include koa-router separately to define routes like above using Koa.

Koa vs Express: Key Differences

Middleware chain

Express middleware chain is callback-based, while Koa’s is Promise-based. This is why we define our errorMiddleware last with Express and handleError first with Koa.

Augmenting Node vs Replacing Node

Express is a web framework for NodeJS. Express augments Node's req and res objects with useful methods and properties.

For example, Express adds things like req.method and req.headers() to the http req object and res.sendFile() to the http res object.

Koa is a middleware framework for NodeJS. Koa replaces or extracts Node's req and res objects with it's own context (ctx).

For example, context.body = result sets the response body for the request.

More lightweight

Koa is lighter than Express. Koa doesn't include router or view engine modules like Express. These modules exist separately and can easily be included. In our examples, you'll notice we had to include koa-router separately for our Koa example whereas this was already included with Express.

Unlike Express, Koa doesn't support routing, templating, sending files, JSONP, etc. out of the box.

Less popular (but growing)

Express is exponentially more popular than Koa. While Koa is growing in popularity, Express remains the de-facto web application framework with over 11 million weekly downloads.

This means you'll find a lot more documentation on Express than Koa. NodeJS developers are more likely to have experience with Express than Koa coming into a project.

Cleaner syntax

Koa leverages Generator functions and async/await for cleaner code. While this helps avoid the "callback hell" commonly experienced with Express, it's less obvious when using async/await with Express in Node v7+.

When to use Express over Koa

Use Express when you're application is browser based and you want support for routing and templating.

Use Express if your development team is new and needs more support / documentation from the community.

When to use Koa over Express

Use Koa when you're application isn't browser based and you don't need support for routing and templating.

Use Koa if you are emphasizing performance (more lightweight). While the performance gains can be negligible, Koa has proven to be slightly faster than Express under certain test conditions.

Conclusion

Remember that the same developers who brought you Express developed Koa. Koa is just a more lightweight version of Express that doesn't come with routing and templating out of the box.

Express is much more mature than Koa. Its community is more established. You will find more documentation/examples on Express than Koa.

While Koa makes writing middleware cleaner, using Express with async/await also avoids "callback hell" and achieves a similar syntax.

There is nothing you can do with one that you can't do with the other.