ルートを個別のファイルに分割する方法を見つけようとしています。
これまでのところこれはありますが、うまくいきません。ただNot found
アクセスしようとするとhttp://localhost:3001/api/things
//server.js
var koa = require('koa');
var app = koa();
var router = require('koa-router');
app.use(router(app));
require('./routes')(app);
// routes.js
module.exports = function *(app){
app.use('/api/things', require('./api/things'));
};
// api/things/index.js
var Router = require('koa-router');
var router = new Router({
prefix: '/api/things'
});
router.get('/', function *(){
this.body = [{ name: 'Foo'}, { name: 'Bar' }];
});
module.exports = router;
これを読んでいる人のために、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
_
routes()
とallowedMethods()
の繰り返しを避けたい場合は、_koa-compose
_を使用してミドルウェアを一緒に構成できます。簡単にするために、_koa-router
_の操作を簡単にするために wrapper を作成しました。それを使用すると、次のようになります。
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
_
そして、それは同じことをします。
var app = require('koa')();
var router = require('./routes');
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000);
var router = require('koa-router')();
router.get('/', function* () {
this.body = 'router test';
});
module.exports = router;
このような何かが動作するはずです:
// 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
私が考える最小限のアプローチ[〜#〜] tj [〜#〜]
koa、koa-route、koa-mount
あなたが好きな方法でマウントした小さな独立したアプリでアプローチ:
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;
Koa 2.xの場合、ネストされたルートとprefixesを見つけて配信します商品、追加パッケージなし。
/ api/dogsおよび/ api/catsが必要な結果であると仮定する。
そのようです:
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;
ここに私が行くことになったものがあります:
//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){
};