Express vs Koa — A Quick Comparison

Athraja Vibhu Jayawardane
3 min readMay 14, 2022

· About Node.js

Node.js has become one of the most trending open-source, cross-platform JavaScript run-time environments since 2009. Many node.js web frameworks were created since then. Each one of the frameworks is bonded to the high demands of the users and developers, like improving productivity, scaling, enhancing speed, and performance capability. Node.js has the advantage of being scalable and lightweight, making it rapidly gain attention among stack developers.

· What is Express?

Express is a code-oriented web framework that seeks to provide developers with a plain, economical, and balanced toolkit for creating web application servers. The API is kept lightweight and maintains a high degree of consistency with the Node.js core API. Because of its understated nature, many ordinary tasks require outside modules.

Having been in improvement since 2009, Express is a strong project with a strong community backing.

· What is Koa?

Koa is a brand-new web application framework created by the team behind Express, which seeks to be a smaller, more expressive, and more robust foundation for web applications and APIs. By leveraging async functions, Koa allows you to drop callbacks and greatly increase the error-handling process.

Koa does not bundle any middleware within its core, and it provides a refined suite of methods that make writing servers quick and pleasurable.

· Express vs Koa — Setting up

The basic setup of Express is plain forward. The Express module returns a module function and then will return a new Express application.

const express = require(‘express’);

const app = express();

const PORT = process.env.PORT || 3000;

const server = app.listen(PORT, () => {

console.log(`Express is listening to http://localhost:${PORT}`);

});

The Koa setup is not much different from Express initialization except it ditches the callback from ES6 function generators and a new async control flow.

const koa = require(‘koa’);

const app = koa();

const PORT = process.env.PORT || 3000;

const server = app.listen(PORT, () => {

console.log(`Koa is listening to http://localhost:${PORT}`);

});

· Express vs Koa — Middleware

Middleware is one of the main concepts that developers use when it comes to playing with Node.js framework. Middleware functions are methods that sit in between requests and responses. They have access to the request and response objects and can run the next middleware after they’re processed.

In Express, registering middleware is as plain as binding into the app object by app.use() function.

app.use((req, res, next) => {

console.log(`Time: ${Date.now()}`);

next();

})

Koa middleware registration is like express, and the difference is that the context object called ‘ctx’ is used instead of the request and responses. Koa functions use the old async/await callbacks.

app.use(async (ctx, next) => {

console.log(`Time: ${Date.now()}`);

await next();

});

· Express vs Koa — Install

To install both, Node.js must be installed first.

Express — npm install express –save

Koa — npm install koa

· Express vs Koa — Console log example

Express:

const express = require(‘express’)

const app = express()

const port = 3000app.get(‘/’, (req, res) => res.send(‘Hello World!’))app.listen(port, () => console.log(`Example app listening on port ${port}!`))

Koa:

const Koa = require(‘koa’);

const app = new Koa();app.use(async ctx => {

ctx.body = ‘Hello World’;

});app.listen(3000);

· Express vs Koa — Performance

Express offers a thin layer of basic web application features, without obfuscating Node.js features that are familiar.

To enhance express performance more, the users must,

Don’t use synchronous functions,

Handle exceptions properly, using try-catch or promises,

Cache request results, so that the app does not repeat the operation to serve the same request constantly,

Run the app in a cluster. The user can increase the performance of a Node.js app considerably by launching a cluster of processes, and

Use a reverse proxy that executes supporting operations on the requests. It can handle error pages, compression, caching, serving files, and load balancing among other things.

With Koa, the user can build up web applications with great implementation. Because the user can stop using callbacks, and deal with errors quicker, Koa itself is a very lightweight framework. It makes the code managing process easier.

It is essential to consider the best practices for having a better performance in Node.js like running things in parallel, using asynchronous APIs in your code, keeping code small and light, and using gzip compression.

· Conclusion

For small-sized and middle-sized projects, personal portfolio projects, and university projects, both express and koa can be good options to consider. Applications like MySpace and PayPal used Express as their middleware framework.

However, going forward as a developer, for large and complex projects, these two frameworks may not be the best tool to use.

--

--