Koa Router with FeathersJS V5
New Version
FeathersJS V5 is out. FeathersJS now uses KoaJS as its default application core instead of ExpressJS, with some slight differences between the two.
For example, I can't generate routed middleware with @feathersjs/cli. Since it's still a KoaJS application, I can still can bring the koa-router to the party.
Use @koa/router in FeathersJS
First, install the @koa/router
package.
npm install @koa/router
# Type Definition
npm install --save-dev @types/koa__router
Then, create a new file src/middlewares/index.ts
in FeatherJS project and add the following code.
import {Application} from '../declarations';
import Router from '@koa/router';
const router = new Router();
export const middlewares = (app: Application) => {
// All middlewares will be registered here
// Register middlewares here, you can also move this middleware to a separate file like `src/middlewares/health.ts`
router.get('/health', async (ctx) => {
ctx.body = 'OK';
});
app.use(router.routes());
}
Finally, register the middleware in src/app.ts
.
import {middlewares} from './middlewares';
// Register middlewares
app.configure(middlewares);