Nodemon
nodemon是一种工具,它可以在检测到目录中的文件更改时通过自动重新启动节点应用程序来帮助开发基于node.js的应用程序。
(1)安装
npm install -g nodemon
// 或
npm install --save-dev nodemon
(2)运行
npx nodemon src/index.js(应用程序)
// 或者在package.json里添加运行命令
{
"scripts": {
"start": "nodemon src/index.js"
}
}
// 运行
npm run start
koa-compose
编写给定的中间件并返回中间件,即整合中间件。
(1)安装
npm install koa-compose
(2)使用
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()
// 使用 koa-compose 打包中间件
const middleware = compose({
koaBody(),
statics(path.join(__dirname, '../public')),
cors(),
jsonutil({ pretty: false, param: 'pretty' }),
helmet()
})
app.use(middleware)
app.use(router())
koa-compress
压缩 koa 中间件
(1) 安装
npm i koa-compress
(2)使用
import koa from 'koa'
import compress from 'koa-compress'
const app = new Koa()
app.use(compress({
filter (content_type) {
return /text/i.test(content_type)
},
threshold: 2048,
gzip: {
flush: require('zlib').constants.Z_SYNC_FLUSH
},
deflate: {
flush: require('zlib').constants.Z_SYNC_FLUSH,
},
br: false // disable brotli
}))
网友评论