在koa v2框架中使用express模块

618 阅读1分钟

github.com/aerchen/koa…

使用方法如下。

const Koa = require('koa')
const c2k = require('koa-connect')

const app = new Koa()

//使用正常模块
app.use(koaMiddlware)

//使用express模块
app.use(c2k(connectMiddlware))

//使用匿名模块
app.use((ctx, next) => {
  console.log('It will continue on to here')
})

app.listen(3000)

注意事项:

It is highly recommended to use a Koa-specific middleware instead of trying to convert an Express version when they're available. There is a non-trivial difference in the Koa and Express designs and you will inevitably run into some issues. This module is a workaround for the specific cases where the differences can be ignored. Additionally, it also enables library authors to write 1 version of their HTTP middleware.

谷歌翻译:

强烈建议使用特定于koa的中间件,而不是尝试转换可用的Express版本。在Koa和Express设计中有一个不小的差异,您将不可避免地遇到一些问题。对于可以忽略差异的特定情况,此模块是一种解决方案。此外,它还允许库作者编写他们的HTTP中间件的一个版本。