合并路由:koa-combine-routers
//安装
npm install koa-combine-routers
//用法
app.js
const Koa = require('koa')
const router = require('./routes')
const app = new Koa()
app.use(router())
routes.js
const Router = require('koa-router')
const combineRouters = require('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
)
module.exports = router
静态资源: koa-static
//安装
npm install koa-static
//用法
onst serve = require('koa-static');
const Koa = require('koa');
const app = new Koa();
// $ GET /package.json
app.use(serve('.'));
// $ GET /hello.txt
app.use(serve('test/fixtures'));
// or use absolute paths
app.use(serve(__dirname + '/test/fixtures'));
app.listen(3000);
console.log('listening on port 3000');
安全头盔: koa-helmet
//安装
npm i koa-helmet
# or:
yarn add koa-helmet
//用法
import Koa from 'koa';
import helmet from 'koa-helmet';
const app = new Koa();
app.use(helmet());
app.use((ctx) => {
ctx.body = "Hello World"
});
app.listen(4000);