Koa Middleware Converter
raw JSON →koa-convert is a utility package designed to bridge compatibility gaps between major versions of the Koa.js web framework. It primarily converts legacy Koa 0.x and 1.x generator-based middleware into modern Koa 2.x promise-based middleware. Conversely, it can also convert modern promise middleware back to legacy generator format, which is useful for enabling newer middleware to run on older Koa 0.x or 1.x applications. The current stable version is 2.0.0, released in 2017. As such, the package is no longer actively developed, and its release cadence has ceased. Its key differentiator is its ability to facilitate incremental migration for existing Koa applications, allowing mixed middleware patterns to coexist. While still functional for its specific purpose, its overall utility for new projects has significantly diminished as Koa 2.x has been the standard for many years.
Common errors
error TypeError: middleware is not a function ↓
convert(): app.use(convert(legacyMiddleware)). error TypeError: app.use() requires a generator function ↓
convert.back(): app.use(convert.back(modernMiddleware)). Warnings
breaking Koa router middleware (e.g., koa-router, koa-route) often reimplement middleware composition internally. `koa-convert` cannot simply convert these types of middleware directly. Specific Koa 2.x compatible versions of routers should be used instead. ↓
gotcha The `koa-convert` package has not been updated since 2017 (v2.0.0). While it remains functional for its intended purpose, its relevance for new Koa projects is minimal as Koa 2.x has been standard for many years. It is primarily useful for migrating older Koa 0.x/1.x applications. ↓
gotcha The package requires Node.js version 10 or greater. Using it with older Node.js versions may lead to unexpected errors or incompatibilities, especially with generator functions or promise-related features. ↓
Install
npm install koa-convert yarn add koa-convert pnpm add koa-convert Imports
- convert wrong
const convert = require('koa-convert')correctimport convert from 'koa-convert' - convert.compose wrong
import { compose } from 'koa-convert'correctimport convert from 'koa-convert'; const composedMiddleware = convert.compose(legacyFn, modernFn) - convert.back
import convert from 'koa-convert'; const legacyFn = convert.back(modernFn)
Quickstart
import Koa from 'koa'; // Assuming Koa v2.x
import convert from 'koa-convert';
const app = new Koa();
// Basic usage: convert a legacy generator middleware
app.use(convert(function * legacyMiddleware (next) {
console.log('Legacy: before next');
yield next;
console.log('Legacy: after next');
}));
// Override app.use to automatically convert all legacy middleware
const _originalUse = app.use;
app.use = x => {
console.log('Intercepting app.use, converting middleware if needed.');
return _originalUse.call(app, convert(x));
};
app.use(async (ctx, next) => {
console.log('Modern: before next');
await next();
console.log('Modern: after next');
ctx.body = 'Hello Koa!';
});
// This legacy middleware will now be implicitly converted by the overridden app.use
app.use(function * anotherLegacyMiddleware (next) {
console.log('Another Legacy: before next');
yield next;
console.log('Another Legacy: after next');
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});