一,整合app.use的中间件koa-compose
本来需要写一大堆app.use的,现在只要写一次了:
// const koa=require('koa')
// const path=require('path')
// const helmet = require('koa-helmet')
// const statics= require('koa-static')
// const router=require('./routes/routes')
import koa from 'koa'
import path from 'path'
import helmet from 'koa-helmet'
import statics from 'koa-static'
import router from './routes/routes'
import koaBody from 'koa-body'
import jsonutil from 'koa-json'
import cors from '@koa/cors'
import compose from 'koa-compose'
const app =new koa()
// app.use……
// app.use(helmet())
// app.use(statics(path.join(__dirname,'../public')))
const middleware=compose([
koaBody(),
statics(path.join(__dirname,'../public')),
cors(),
jsonutil({
pretty:false,param:'pretty'}),
helmet()
])
app.use(middleware)
app.use(router())
app.listen(3000)