zhengwei1949 / myblog

个人博客
10 stars 6 forks source link

express开启gzip注意事项 #117

Open zhengwei1949 opened 6 years ago

zhengwei1949 commented 6 years ago
const express = require('express')
const app = express()
const path = require('path')
const compression = require('compression')
//静态资源配置
function shouldCompress(req, res) {
    return true;
}
app.use(compression({ threshold: 0, filter: shouldCompress }));
// app.use(express.compress());
app.use('/public',express.static(path.join(__dirname,'./public')))
//配置模板引擎
app.set('view engine','ejs');
//设置模板引擎目录
app.set('views','./views');//默认如果是views的话可以不写
//开启gzip压缩

app.get('/', (req, res) => {
    // res.send('ok')
    const data = {name:'itcast'}
    res.render('index',data)
})
app.get('/json', (req, res) => {
    // res.send('ok')
    const data = { name: 'itcast' }
    res.json(data)
})
app.get('/jsonp', (req, res) => {
    // res.send('ok')
    const data = { name: 'itcast' }
    res.jsonp(data)
})
app.listen(3000,()=>{
    console.log('http://localhost:3000')
})
zhengwei1949 commented 6 years ago

threshold: 0, filter: shouldCompress一定要加才能出效果