Skip to content

Commit 9cd9743

Browse files
committed
Feature: 增加compression压缩
1 parent 38660ee commit 9cd9743

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const proxy = new Proxy({
3030
host: '0.0.0.0'
3131
port: 8080
3232
},
33+
compression: true, // gzip默认为true
3334
redirect: { //重定向
3435
'/a/b': {
3536
target: '/c/d',

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
const express = require('express')
22
const log4js = require('log4js')
3+
const compression = require('compression')
34
const history = require('connect-history-api-fallback')
45
const httpproxy = require('./libs/proxy')
56
const http = require('http')
67
const https = require('https')
78
const qs = require('querystring')
89
class Proxy {
910
constructor({
10-
bind = { host: '0.0.0.0', port: 3000 }, web = { dir: `${__dirname}/public` }, proxy, redirect
11+
bind = { host: '0.0.0.0', port: 3000 }, web = { dir: `${__dirname}/public` }, proxy, redirect, compression = true
1112
}) {
1213
this.bind = bind
1314
this.web = web
1415
this.proxy = proxy
1516
this.redirect = redirect
1617
this.app = express()
18+
this.compression = compression
1719
}
1820

1921
start() {
@@ -23,6 +25,9 @@ class Proxy {
2325
})
2426
const logger = log4js.getLogger()
2527
this.app.use(log4js.connectLogger(logger));
28+
if (this.compression) {
29+
this.app.use(compression())
30+
}
2631
if(Array.isArray(this.proxy)){
2732
for(let p of this.proxy) {
2833
this.app.use(p.path, httpproxy({
@@ -86,6 +91,7 @@ class Proxy {
8691
})
8792
}
8893
}
94+
8995
this.app.use(history())
9096
this.app.use(express.static(this.web.dir))
9197
this.app.listen(this.bind.port,this.bind.host)

test/test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const proxy = new Proxy({
1414
bind:{
1515
port:8080
1616
},
17+
web: {
18+
dir: 'dist'
19+
},
20+
compression: true,
1721
redirect: {
1822
'/a/:id': {
1923
target: '/c/d',

yarn.lock

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,26 @@ component-emitter@^1.2.1:
334334
resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.2.1.tgz#137918d6d78283f7df7a6b7c5a63e140e69425e6"
335335
integrity sha1-E3kY1teCg/ffemt8WmPhQOaUJeY=
336336

337+
compressible@~2.0.16:
338+
version "2.0.17"
339+
resolved "http://repo.know.org:8081/compressible/-/compressible-2.0.17.tgz#6e8c108a16ad58384a977f3a482ca20bff2f38c1"
340+
integrity sha512-BGHeLCK1GV7j1bSmQQAi26X+GgWcTjLr/0tzSvMCl3LH1w1IJ4PFSPoV5316b30cneTziC+B1a+3OjoSUcQYmw==
341+
dependencies:
342+
mime-db ">= 1.40.0 < 2"
343+
344+
compression@^1.7.4:
345+
version "1.7.4"
346+
resolved "http://repo.know.org:8081/compression/-/compression-1.7.4.tgz#95523eff170ca57c29a0ca41e6fe131f41e5bb8f"
347+
integrity sha512-jaSIDzP9pZVS4ZfQ+TzvtiWhdpFhE2RDHz8QJkpX9SIpLq88VueF5jJw6t+6CUQcAoA6t+x89MLrWAqpfDE8iQ==
348+
dependencies:
349+
accepts "~1.3.5"
350+
bytes "3.0.0"
351+
compressible "~2.0.16"
352+
debug "2.6.9"
353+
on-headers "~1.0.2"
354+
safe-buffer "5.1.2"
355+
vary "~1.1.2"
356+
337357
connect-history-api-fallback@^1.5.0:
338358
version "1.6.0"
339359
resolved "https://registry.yarnpkg.com/connect-history-api-fallback/-/connect-history-api-fallback-1.6.0.tgz#8b32089359308d111115d81cad3fceab888f97bc"
@@ -1345,6 +1365,11 @@ micromatch@^3.1.10:
13451365
snapdragon "^0.8.1"
13461366
to-regex "^3.0.2"
13471367

1368+
"mime-db@>= 1.40.0 < 2":
1369+
version "1.42.0"
1370+
resolved "http://repo.know.org:8081/mime-db/-/mime-db-1.42.0.tgz#3e252907b4c7adb906597b4b65636272cf9e7bac"
1371+
integrity sha512-UbfJCR4UAVRNgMpfImz05smAXK7+c+ZntjaA26ANtkXLlOe947Aag5zdIcKQULAiF9Cq4WxBi9jUs5zkA84bYQ==
1372+
13481373
mime-db@~1.38.0:
13491374
version "1.38.0"
13501375
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.38.0.tgz#1a2aab16da9eb167b49c6e4df2d9c68d63d8e2ad"
@@ -1520,6 +1545,11 @@ on-finished@~2.3.0:
15201545
dependencies:
15211546
ee-first "1.1.1"
15221547

1548+
on-headers@~1.0.2:
1549+
version "1.0.2"
1550+
resolved "http://repo.know.org:8081/on-headers/-/on-headers-1.0.2.tgz#772b0ae6aaa525c399e489adfad90c403eb3c28f"
1551+
integrity sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==
1552+
15231553
optionator@^0.8.1:
15241554
version "0.8.2"
15251555
resolved "https://registry.yarnpkg.com/optionator/-/optionator-0.8.2.tgz#364c5e409d3f4d6301d6c0b4c05bba50180aeb64"

0 commit comments

Comments
 (0)