@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 thekoa option:
Middleware Patterns
- Before Middleware
- After Middleware
- Combined
Middleware in the
before array runs before the service method:Context and Feathers Params
Koa’sctx.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
- listen()
- setup()
- teardown()
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 instancekoaApp- Optional existing Koa app
rest(options?)
Configures the REST transport. Options:formatter- Custom response formatter middlewareauthentication- Authentication settingsservice- Authentication service namestrategies- Array of strategy names
errorHandler()
Koa error handling middleware that catches errors and formats them as JSON.bodyParser(options?)
Parses request bodies. Wrapskoa-body.
Options: See koa-body documentation
cors(options?)
Enables CORS. Wraps@koa/cors.
Options: See @koa/cors documentation