项目作者: xingxingted

项目描述 :
Use a Koa/Express middleware in an Express/Koa app
高级语言: JavaScript
项目地址: git://github.com/xingxingted/koa-to-express.git
创建时间: 2017-05-22T17:05:38Z
项目社区:https://github.com/xingxingted/koa-to-express

开源协议:MIT License

下载


koa-to-express

Build Status
codecov
npm version

Use a Koa/Express middleware in an Express/Koa app.

It could convert a middleware to another form between Express and koa middleware.

Requirement

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.

Installation

Npm 5.x

  1. npm install koa-to-express [-P]

Others

  1. npm install koa-to-express -S

Usage

  1. const k2e = require('koa-to-express');
  2. const koaMiddleware = (ctx, next) => {
  3. ctx.body = 'hello world';
  4. return next();
  5. };
  6. require('express')().use(k2e(koaMiddleware)).listen(3000);
  7. // curl localhost:3000/
  8. // output: hello world

API

koaToExpress[.koaToExpress](middleware)

  • middleware : 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)).

koaToExpress.expressToKoa(middleware)

  • middleware : Function|Array<Function>

  • Convert Express middleware(s) into a Koa middleware and return it.

License

MIT