How can I split my koa routes into separate files?

前端 未结 7 1167
一向
一向 2021-01-30 04:20

I\'m trying to figure out how to split my routes into separate files.

I have this so far, but it doesn\'t work. I just get Not found when I try to access

相关标签:
7条回答
  • 2021-01-30 04:46

    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;
    
    0 讨论(0)
  • 2021-01-30 04:46

    Here's what I ended up going with:

    //server.js
    'use strict';
    
    var koa = require('koa');
    var app = koa();
    var serve = require('koa-static');
    var path = require('path');
    var router = require('koa-router');
    var cfg = require('./config');
    var mw = require('./middleware');
    
    app.use(serve(path.resolve(__dirname, '../client')));
    app.use(mw.requestTime('Response-time'));
    app.use(router(app));
    app.use(cfg.db.connect);
    
    require('./routes')(app);
    
    app.get('/api', function *(){
      this.body = 'Welcome to API v1';
    });
    
    app.use(cfg.db.close);
    app.listen(cfg.env.port);
    
    
    
    //routes.js
    module.exports = function (app){
      app.use(require('./api/things').routes());
    };
    
    
    // api/things/index.js
    var Router = require('koa-router');
    
    var router = new Router({
      prefix: '/api/things'
    });
    
    var ctrl = require('./controllers');
    
    router.get('/', ctrl.list);
    router.get('/:id', ctrl.get);
    router.post('/', ctrl.post);
    router.put('/:id', ctrl.put);
    router.del('/:id', ctrl.del);
    
    module.exports = router;
    
    
    // api/things/controllers.js
    
    var r = require('rethinkdb');
    var http = require('http');
    var parse = require('co-body');
    var ctrl = module.exports = {};
    
    ctrl.list = function *(next){
    };
    
    ctrl.get = function *(next){
    };
    
    ctrl.post = function *(next){
    };
    
    ctrl.put = function *(next){
    };
    
    ctrl.del = function *(next){
    };
    
    0 讨论(0)
  • 2021-01-30 04:50

    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;
    
    0 讨论(0)
  • 2021-01-30 05:06

    Minimalistic approach which i think TJ kept in mind when he did

    koa, koa-route, koa-mount

    Approach with small independent apps which mounted the way you like then:

    index.js

    var app = require('./app')
    app.listen(3000);
    

    app.js

    const Koa = require('koa')
    const _ = require('koa-route')
    const mount = require('koa-mount')
    const app = new Koa()
    const pets = require('./pets')
    
    // sub apps
    app.use(mount('/pets', pets))
    
    // root app
    app.use(_.get('/', function(){
      this.body = "hello";
    }))
    
    module.exports = app;
    

    pets.js

    var Koa = require('koa');
    var _ = require('koa-route');
    var app = new Koa();
    
    
    app.use(_.get('/', ctx => ctx.body = "pets" ));
    app.use(_.get('/:name', (ctx, name) => ctx.body = "pet: "+ name));
    
    module.exports = app;
    
    0 讨论(0)
  • 2021-01-30 05:07

    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
    
    0 讨论(0)
  • 2021-01-30 05:12

    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.

    0 讨论(0)
提交回复
热议问题