Use gzip compression
Gzip compressing can greatly decrease the size of the response body and hence increase the speed of a web app. Use the compression middleware for gzip compression in your Express app. For example:
const express = require('express')
const app = express()
const port = 3000
const compression = require('compression')
app.use(compression()) // 需要位于 express.static 前面,否则不起作用
app.use(express.static('public')) // public 文件夹中的静态资源都将被做 gzip 处理
// app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`http://localhost:${port}`))
https://expressjs.com/en/advanced/best-practice-performance.html
网友评论