@glandjs/koa@1.1.0-beta
@glandjs/koa
The Koa 3 transport — a native router, native extended verbs and a real promise onion with no caveats about next().
@glandjs/koa binds your controllers and channels to a Koa 3
application. It is the adapter with the least to explain: Koa’s model is the model this
layer already uses.
pnpm add @glandjs/core @glandjs/common @glandjs/events @glandjs/http @glandjs/koa koa @koa/router reflect-metadataAttaching it
Section titled “Attaching it”import { GlandFactory } from '@glandjs/core'import { Controller, Module } from '@glandjs/common'import { Get } from '@glandjs/http'import { KoaBroker, type KoaContext } from '@glandjs/koa'
@Controller('/products')export class ProductController { @Get('/:id') async find(ctx: KoaContext) { return { product: await ctx.call('db:product:find', { id: ctx.params.id }) } }}
@Module({ controllers: [ProductController] })export class AppModule {}
const { app } = await GlandFactory.create(AppModule)const http = app.connectTo(KoaBroker, { poweredBy: false })
http.bodyParser({ limit: 1_000_000 })http.listen(3000)Exports
Section titled “Exports”| Symbol | Description |
|---|---|
KoaBroker<TEvents> |
The broker you pass to connectTo(). |
KoaContext<TEvents> |
The context your handlers receive. |
KoaCore<TEvents> |
The application object, plus http.koa. |
KoaAdapter<TEvents> |
The transport implementation. |
The real onion
Section titled “The real onion”Koa is an onion, so await next() genuinely suspends until everything downstream has
run, and a downstream throw reaches an upstream catch. This is the behaviour the
other four adapters work to reproduce.
http.use(async (ctx, next) => { const started = Date.now() await next() console.log(`${ctx.method} ${ctx.path} ${Date.now() - started}ms`)})http.koa is the raw instance. The adapter mounts @koa/router itself — Koa ships no
router — and carries no prefix on it, because the base class has already applied
setGlobalPrefix.
Everything is native
Section titled “Everything is native”@koa/router accepts any method string, so every extended and WebDAV verb routes
natively — including MKWORKSPACE and UPDATE, which Fastify cannot do.
| Feature | Needs | Install |
|---|---|---|
| Body parsing | koa-bodyparser |
pnpm add koa-bodyparser |
| Static files | koa-static |
pnpm add koa-static |
| Multipart | @koa/multer |
pnpm add @koa/multer |
| Views | @koa/views |
pnpm add @koa/views |
| Signed cookies | a signed-cookie middleware + http.koa.keys |
— |
What it cannot do
Section titled “What it cannot do”- Multipart.
enableTypesis['json', 'form', 'text']; mount@koa/multerthroughuseRaw(). poweredBy: falseis a no-op: Koa sets noX-Powered-Byof its own.signedCookiesis{}unless a signed-cookie middleware is mounted.sendFilebyte ranges are unsupported;maxAgeandlastModified: falsework.