Use a Koa/Express middleware in an Express/Koa app
Use a Koa/Express middleware in an Express/Koa app.
It could convert a middleware to another form between Express and koa middleware.
Koa-to-express requires koa@2.0.0 and express@4.0.0 or higher.
Node 7.6+ is required if you want to use ES2015’s async/await syntax.
Please go to koa-to-express@1.x if you want to use koa1.
Npm 5.x
npm install koa-to-express [-P]
Others
npm install koa-to-express -S
const k2e = require('koa-to-express');
const koaMiddleware = (ctx, next) => {
ctx.body = 'hello world';
return next();
};
require('express')().use(k2e(koaMiddleware)).listen(3000);
// curl localhost:3000/
// output: hello world
Function|Array<Function>
Convert Koa middleware(s) into an Express middleware and return it.
Note that the second parameter (usually named as “next”) passed to the last one of the middlewares is () => Promise.resolve()
, because the cascading categories between Koa and Express are different and the end of the rest Express middlewares executing can’t be informed back.
It means that, resolving or rejecting the Koa middleware(s), which will return a resolved or rejected promise, will call the next middleware (as calling the Express’s next()
) or throw out an error (as calling the next(err)
).
Function|Array<Function>
MIT