美文网首页
NodeJS 开启 gzip 压缩

NodeJS 开启 gzip 压缩

作者: 不知道的是 | 来源:发表于2019-01-21 11:25 被阅读0次

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

相关文章

网友评论

      本文标题:NodeJS 开启 gzip 压缩

      本文链接:https://www.haomeiwen.com/subject/ryzqjqtx.html