koa-final-response
The very outer middleware of Koa to handle every response of every request.
Usage
Installation
yarn add koa-final-response
or npm install koa-final-response --save
Example
const path = ;// Error object, see https://github.com/hapijs/boom// We recommand to use Boom as the standard object for error responsesconst Boom = ;const Koa = ;const mountRoutes = ;const finalResp = ; const app = ;// this middleware should be added before router worksapp;// mount routes, see https://github.com/Maples7/koa-mount-routes;app;
How to return results
-
For normal responses, you can pass your result data to
ctx.body
directly just following the standard Koa way; -
For error responses, we recommend you use Boom to pass those expected errors like
throw Boom.unauthorized('invalid password')
. Besides, any unexpected errors and non-Boom errors thrown by yourself will also be catched and handled well as you want.
Responses
-
Normal response
-
Error response
-
404 response
API
app;
You are welcomed to review test.js and controllers dir in this project for more information of usage.