How can I split my koa routes into separate files?

server.js

var app = require('koa')();
var router = require('./routes');
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000);

routes.js

var router = require('koa-router')();
router.get('/', function* () {
    this.body = 'router test';
});
module.exports = router;

For Koa 2.x I find nested routes with prefixes to deliver the goods, with no additional packages.

Assuming /api/dogs and /api/cats are the required outcomes.

Like so:

app.js

import Koa from 'koa';
import Router from 'koa-router';
import apiRouter from './routes/apiRouter';

const app = new Koa();

app.use(apiRouter.routes(), apiRouter.allowedMethods());

app.listen(3000);

export default app;

routes/apiRouter.js

import Router from 'koa-router'

import dogsRouter from './dogsRouter'
import catsRouter from './catsRouter'

const apiRouter = new Router({ prefix: '/api' })

const nestedRoutes = [dogsRouter, catsRouter]
for (var router of nestedRoutes) {
    apiRouter.use(router.routes(), router.allowedMethods())
}

export default apiRouter;

routes/dogsRouter.js

import Router from 'koa-router'

const dogsRouter = new Router({ prefix: '/dogs' });

dogsRouter.get('/', async (ctx, next) => {
  ctx.body = 'Dogs be here'
});

export default dogsRouter;

routes/catsRouter.js

import Router from 'koa-router'

const catsRouter = new Router({ prefix: '/cats' });

catsRouter.get('/', async (ctx, next) => {
  ctx.body = 'Cats be here'
});

export default catsRouter;

Something like this should work:

// app.js
var koa = require('koa'),
    app = koa();

require('./routes1')(app);
require('./routes2')(app);

app.listen(3000);

// routes1.js
var Router = require('koa-router');
function register (app) {
  var router = new Router({
    prefix: '/api'
  });
  router.get('/', ...); // responds to "/api"
  router.get('/messages', ...); // responds to "/api/messages"
  app.use(router.routes());
  app.use(router.allowedMethods());
}

module.exports = register

// routes2.js
var Router = require('koa-router');
function register (app) {
  var router = new Router();
  router.get('/', ...); // responds to "/"
  app.use(router.routes());
  app.use(router.allowedMethods());
}

module.exports = register


EDIT: I've updated the code examples below because the koa-router package on npm is no longer maintained. The Koa team has made an official fork of it under the name @koa/router.


For anyone reading this, who is curious on how to do this in Koa 2.X:

app.js

import Koa from 'koa'
import rootRouter from './routes/root'
import userRouter from './routes/user'

const app = new Koa()

app.use(rootRouter.routes())
app.use(rootRouter.allowedMethods())
app.use(userRouter.routes())
app.use(userRouter.allowedMethods())

export default app

routes/root.js

import Router from '@koa/router'
const router = new Router()

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

export default router

routes/user.js

import Router from '@koa/router'
const router = new Router({ prefix: '/user' })

router.get('/', async (ctx, next) => {
  ctx.body = 'Some User'
})

export default router

If you want to avoid the repetition with the routes() and the allowedMethods(), you can use koa-compose to compose the middleware together. For simplicity, I made a wrapper around it to simplify working with koa-router. Using it would look something like this:

app.js

import Koa from 'koa'
import router from './routes'

const app = new Koa()

app.use(router())

export default app  

routes/index.js

import combineRouters from 'koa-combine-routers'
import rootRouter from './root'
import userRouter from './user'

const router = combineRouters(
  rootRouter,
  userRouter
)

export default router

And it would do the same thing.