nodejs background series-fourth-koa (four)

One, integrate the middleware koa-compose of app.use

Originally needed to write a lot of app.use, now it only needs to be written once:

// 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)

Guess you like

Origin blog.csdn.net/weixin_42349568/article/details/114994420