Skip to main content
The @feathersjs/koa package provides integration between Feathers and Koa, bringing async/await middleware patterns and lightweight HTTP handling to your Feathers services.

Installation

Basic Setup

1

Create the Koa application

The Koa integration wraps a Feathers application with Koa functionality:
2

Add body parsing middleware

Enable request body parsing for POST/PUT/PATCH requests:
3

Configure REST transport

Enable the REST transport to expose services via HTTP:
4

Register services

Services are automatically exposed as REST endpoints:
5

Add error handling

Use the error handler middleware to catch and format errors:
6

Start the server

The listen method automatically calls setup() on all services:

Complete Example

Service Middleware

Koa middleware can be added before and after service calls using the koa option:

Middleware Patterns

Middleware in the before array runs before the service method:

Context and Feathers Params

Koa’s ctx.feathers object contains Feathers-specific parameters:

Authentication

Integrate Feathers authentication with Koa middleware:

Authentication Configuration

Error Handling

Koa error handling is done through middleware:

Static Files

Serve static files using Koa’s static middleware:

Custom Response Formatting

Customize how service results are sent as HTTP responses:

Using Existing Koa App

Integrate Feathers into an existing Koa application:

Pure Koa Middleware

You can mix Feathers services with pure Koa middleware:

Lifecycle Methods

The listen() method starts the server and calls setup() on all services:

TypeScript Support

Middleware Execution Order

Understanding middleware order is crucial in Koa:

API Reference

koa(feathersApp?, koaApp?)

Wraps a Feathers application with Koa. Parameters:
  • feathersApp - A Feathers application instance
  • koaApp - Optional existing Koa app
Returns: Combined Feathers + Koa application

rest(options?)

Configures the REST transport. Options:
  • formatter - Custom response formatter middleware
  • authentication - Authentication settings
    • service - Authentication service name
    • strategies - Array of strategy names

errorHandler()

Koa error handling middleware that catches errors and formats them as JSON.

bodyParser(options?)

Parses request bodies. Wraps koa-body. Options: See koa-body documentation

cors(options?)

Enables CORS. Wraps @koa/cors. Options: See @koa/cors documentation