在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:koajs/convert开源软件地址:https://github.com/koajs/convert开源编程语言:JavaScript 100.0%开源软件介绍:koa-convertConvert Koa legacy (0.x & 1.x) generator middleware to modern promise middleware (2.x). It could also convert modern promise middleware back to legacy generator middleware (useful to help modern middleware support Koa v0.x or v1.x). NoteRouter middleware is special case here. Because it reimplements middleware composition internally, we cannot not simply convert it. You may use following packages for routing, which are koa 2.x ready now: Installation# npm ..
$ npm i koa-convert
# yarn ..
$ yarn add koa-convert Usageconst Koa = require('koa') // koa v2.x
const convert = require('koa-convert')
const app = new Koa()
app.use(modernMiddleware)
app.use(convert(legacyMiddleware))
app.use(convert.compose(legacyMiddleware, modernMiddleware))
function * legacyMiddleware (next) {
// before
yield next
// after
}
function modernMiddleware (ctx, next) {
// before
return next().then(() => {
// after
})
} Distinguish legacy and modern middlewareIn koa 0.x and 1.x (without experimental flag), Therefore, we can distinguish legacy and modern middleware with Migration
You can use following snippet to make migration easier. const _use = app.use
app.use = x => _use.call(app, convert(x)) The above snippet will override Therefore, you can have both Complete example: const Koa = require('koa') // v2.x
const convert = require('koa-convert')
const app = new Koa()
// ---------- override app.use method ----------
const _use = app.use
app.use = x => _use.call(app, convert(x))
// ---------- end ----------
app.use(modernMiddleware)
// this will be converted to modern promise middleware implicitly
app.use(legacyMiddleware)
function * legacyMiddleware (next) {
// before
yield next
// after
}
function modernMiddleware (ctx, next) {
// before
return next().then(() => {
// after
})
} API
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论