---
title: "Koa Router with FeathersJS V5"
date: 2023-04-14T09:59:56.000Z
author: Z.SHINCHVEN
tags: [KoaJS, FeathersJS]
canonical: https://atlassc.net/2023/04/14/koa-router-with-feathersjs-v5
---
## New Version

[FeathersJS](https://feathersjs.com) 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](https://www.npmjs.com/package/@feathersjs/cli). Since it's still a KoaJS application, I can still can bring the [koa-router](https://www.npmjs.com/package/koa-router) to the party.

## Use @koa/router in FeathersJS

First, install the `@koa/router` package.

```bash
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.

```ts
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`.

```ts
import {middlewares} from './middlewares';

// Register middlewares
app.configure(middlewares);
```
