express and koa are both popular Node.js frameworks used to build web servers and APIs. express is a mature, minimalistic framework that has been the industry standard for years, offering a robust set of features for routing and middleware. koa, created by the same team behind express, is a modern alternative designed to be smaller and more expressive, leveraging async/await to handle middleware flow without callbacks. Both tools help developers manage HTTP requests, but they differ significantly in architecture and coding style.
Both express and koa are Node.js frameworks designed to handle HTTP server logic, but they approach middleware and request handling in fundamentally different ways. express relies on a callback-based model that has served the industry for over a decade. koa was built by the same creators to fix specific design flaws in express, using modern async functions to create a cleaner flow. Let's look at how they compare in real engineering scenarios.
express uses a callback-based middleware system.
next() to pass control to the next function.next(), the request hangs.next(err).// express: Callback-based middleware
app.use((req, res, next) => {
console.log('Time:', Date.now());
next(); // Must call next to continue
});
app.use((req, res, next) => {
if (error) {
return next(error); // Pass error to error handler
}
res.send('OK');
});
koa uses async/await for middleware.
await next() to pass control down the stack.// koa: Async/await middleware
app.use(async (ctx, next) => {
console.log('Time:', Date.now());
await next(); // Await to continue
});
app.use(async (ctx, next) => {
try {
await someAsyncTask();
ctx.body = 'OK';
} catch (err) {
ctx.status = 500;
ctx.body = err.message;
}
});
express separates the request and response into two distinct objects.
req holds incoming data.res handles outgoing data.// express: Separate req and res
app.get('/user', (req, res) => {
const id = req.params.id;
res.status(200).json({ id });
});
koa combines these into a single context object ctx.
ctx.request holds incoming data.ctx.response handles outgoing data.ctx.body make code shorter.// koa: Single context object
app.use(async (ctx) => {
const id = ctx.params.id;
ctx.status = 200;
ctx.body = { id };
});
express relies on a specific error-handling middleware signature.
(err, req, res, next).next() is not called correctly.// express: Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});
// Async error handling requires care
app.get('/', async (req, res, next) => {
try {
await doWork();
res.send('OK');
} catch (e) {
next(e); // Must manually pass to next
}
});
koa uses standard try/catch blocks inside middleware.
// koa: Native try/catch
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
ctx.status = err.status || 500;
ctx.body = err.message;
ctx.app.emit('error', err, ctx);
}
});
express has routing built into the core.
// express: Built-in routing
const express = require('express');
const app = express();
app.get('/users', (req, res) => {
res.json({ users: [] });
});
app.post('/users', (req, res) => {
res.status(201).json({ id: 1 });
});
koa does not include routing in the core.
@koa/router.// koa: External routing package
const Koa = require('koa');
const Router = require('@koa/router');
const app = new Koa();
const router = new Router();
router.get('/users', (ctx) => {
ctx.body = { users: [] };
});
app.use(router.routes());
While the architectures differ, both frameworks share core goals and behaviors.
http or https modules.// Both can listen on a port
// express
app.listen(3000);
// koa
app.listen(3000);
// express: body-parser (now built-in)
app.use(express.json());
// koa: koa-bodyparser
const bodyParser = require('koa-bodyparser');
app.use(bodyParser());
// express: View setup
app.set('view engine', 'pug');
// koa: View setup (via middleware)
const views = require('koa-views');
app.use(views(__dirname + '/views', { extension: 'pug' }));
// Testing both with supertest
const request = require('supertest');
// express test
request(app).get('/users').expect(200);
// koa test
request(app.callback()).get('/users').expect(200);
| Feature | Shared by Express and Koa |
|---|---|
| Runtime | 🟢 Node.js |
| Purpose | 🌐 HTTP Server & API |
| Extensibility | 🔌 Middleware-based |
| Testing | 🧪 Supertest compatible |
| Views | 🎨 Template engine support |
| Feature | express | koa |
|---|---|---|
| Middleware | 🧵 Callback + next() | ⏳ Async/Await + await next() |
| Request/Response | 📦 Separate req and res | 📦 Single ctx object |
| Error Handling | ⚠️ next(err) pattern | ⚠️ Native try/catch |
| Routing | 🗺️ Built-in | 🗺️ External (@koa/router) |
| Code Style | 📜 Traditional | 🆕 Modern ES6+ |
express is like a reliable workhorse 🐴 — it has been around for a long time, has a plugin for everything, and is the default choice for many teams. It is perfect for standard REST APIs, legacy projects, or when you need to hire developers who already know the stack.
koa is like a modern sports car 🏎️ — it is lighter, faster to write in, and uses modern JavaScript features to reduce boilerplate. It is best for new projects where the team values clean code and wants to avoid callback patterns.
Final Thought: Both frameworks are actively maintained and capable. If you are starting fresh and love async/await, koa offers a nicer developer experience. If you need stability and the largest possible library of middleware, express remains the king.
Choose express if you need a stable, battle-tested framework with a massive ecosystem of plugins and middleware. It is ideal for teams that value convention, extensive documentation, and quick setup for traditional REST APIs. The callback-based middleware model is well-understood, making it easier to find developers familiar with the codebase.
Choose koa if you prefer a modern codebase built on async/await, which eliminates callback hell and improves error handling. It is suitable for projects where you want a lighter core and more control over middleware composition. This framework works well for teams comfortable with modern JavaScript features who want to avoid legacy patterns.
Fast, unopinionated, minimalist web framework for Node.js.
This project has a Code of Conduct.
import express from 'express'
const app = express()
app.get('/', (req, res) => {
res.send('Hello World')
})
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000')
})
This is a Node.js module available through the npm registry.
Before installing, download and install Node.js. Node.js 18 or higher is required.
If this is a brand new project, make sure to create a package.json first with
the npm init command.
Installation is done using the
npm install command:
npm install express
Follow our installing guide for more information.
PROTIP Be sure to read the migration guide to v5
The quickest way to get started with express is to utilize the executable express(1) to generate an application as shown below:
Install the executable. The executable's major version will match Express's:
npm install -g express-generator@4
Create the app:
express /tmp/foo && cd /tmp/foo
Install dependencies:
npm install
Start the server:
npm start
View the website at: http://localhost:3000
The Express philosophy is to provide small, robust tooling for HTTP servers, making it a great solution for single page applications, websites, hybrids, or public HTTP APIs.
Express does not force you to use any specific ORM or template engine. With support for over 14 template engines via @ladjs/consolidate, you can quickly craft your perfect framework.
To view the examples, clone the Express repository:
git clone https://github.com/expressjs/express.git --depth 1 && cd express
Then install the dependencies:
npm install
Then run whichever example you want:
node examples/content-negotiation
The Express.js project welcomes all constructive contributions. Contributions take many forms, from code for bug fixes and enhancements, to additions and fixes to documentation, additional tests, triaging incoming pull requests and issues, and more!
See the Contributing Guide for more technical details on contributing.
If you discover a security vulnerability in Express, please see Security Policies and Procedures.
To run the test suite, first install the dependencies:
npm install
Then run npm test:
npm test
For information about the governance of the express.js project, see GOVERNANCE.md.
The original author of Express is TJ Holowaychuk