1.安裝koa-compress中間件
cnpm i koa-compress -S
2.配置koa-compress中間件
const Koa = require('koa');
const app = new Koa();
const compress = require('koa-compress');
const options = {
threshold: 1024
};
app.use(compress(options));
app.use(async ctx => {
ctx.type = 'html';
ctx.body =
`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<div>hello world</div>
</body>
</html>`;
});
const hostName = '127.0.0.1';
const port = 8888;
app.listen(port, hostName, () => {
console.log(`服務運行在http://${hostName}:${port}`);
});
console.log('服務啓動成功');