3

I decided to split my routers by their purpose, so they look like this:

routers/homeRouter.ts

import * as Router from 'koa-router';

const router: Router = new Router();

router
    .get('/', async (ctx, next) => {
        ctx.body = 'hello world';
    });

export = router;

routers/userRouter.ts

import * as Router from 'koa-router';
import UserController = require('../controller/userController');

const router: Router = new Router(
    {
        prefix: 'users'
    }
);

var userController = new UserController();

router
    .post('/user/:email/:password', userController.signUp);

export = router;

With this, my app.ts has to import each of the routers one by one like this:

app.ts

import * as Koa from 'koa';
import * as homeRouter from './routers/homeRouter';
import * as userRouter from './routers/userRouter';
const app: Koa = new Koa();
app
    .use(homeRouter.routes())
    .use(homeRouter.allowedMethods());
app
    .use(userRouter.routes())
    .use(userRouter.allowedMethods());
app.listen(3000);

But what I want is this:

app.ts

import * as Koa from 'koa';
import * as routers from './routers';
const app: Koa = new Koa();
app
    .use(routers.routes())
    .use(routers.allowedMethods());
app.listen(3000);

I do not know how to export the routers to achieve this. Can anyone help?

Fuchur Kool
  • 53
  • 2
  • 7

3 Answers3

4

You can have something like this:

userRouter.ts

import * as Router from 'koa-router';

const router = new Router();
router.get('/', list);

...

export default router.routes();

routes.ts

import * as Router from 'koa-router';


import UserRouter from './userRouter';
import HomeRouter from './homeRouter';

const apiRouter = new Router({ prefix: '/api'});
apiRouter.use('/users', UserRouter);
apiRouter.use('/home', HomeRouter);

export default apiRouter.routes();

You can implement each router separately as you have done, then generate a new router which includes all of your routers. Then you can include it in your app.ts file.

Arda Keskiner
  • 772
  • 7
  • 23
1

Meseret is a nice typescript library I wrote that can help you manage your Koa server routers, middleware and much more.

E.g. You can replace your app.ts with this.

import { ServerApp } from 'meseret'

import HomeRouter from './routers/homeRouter'
import UserRouter from './routers/userRouter'

new ServerApp({
  name: 'Your App Name',
  httpServers: [
    { port: 3000 }
  ],
  routers: [
    HomeRouter,
    UserRouter
  ]
}).start() // returns a Promise

All you have to do is just import your koa-router Routers and add them to the list of routers in the ServerApp.

Meseret helps you do much more than manage routers. It has built in support for Koa sessions, static caching, public directory serving, response compression and more. There's also, as expected, an option that lets you add your own Koa middleware to the app.

Plus, it can connect to MongoDB and help manage your Mongoose models (even by adding static type support to your mongoose models--the paths, methods and statics--if you use the ModelFactory).

Socket.IO web sockets are also supported in the config.

All these features are managed in a single ServerApp instance.

Find out more at meseret's Github page.

I hope it helps you manage your distributed node server project.

Kaleab
  • 56
  • 1
  • 2
1

I made a utility awhile back for combining multiple instances of koa-router into a single middleware, and I just now added a TypeScript definition file, so it should be usable for TS folks. Just npm install koa-combine-routers and use it like so:

Usage:

app.js

import * as Koa from 'koa'
import router from './router'

const app = new Koa()

app.use(router())

routes.js

import * as Router from 'koa-router'
import * as combineRouters from 'koa-combine-routers'

const dogRouter = new Router()
const catRouter = new Router()

dogRouter.get('/dogs', async ctx => {
  ctx.body = 'ok'
})

catRouter.get('/cats', async ctx => {
  ctx.body = 'ok'
})

const router = combineRouters(
  dogRouter, 
  catRouter
)

export default router

This will use both .routes() and .allowedMethods().

Saad
  • 49,729
  • 21
  • 73
  • 112