-
Notifications
You must be signed in to change notification settings - Fork 0
/
advanced-pipe.js
95 lines (91 loc) · 2.57 KB
/
advanced-pipe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const app = require('express')()
const request = require('request')
const cheerio = require('cheerio')
const concat = require('concat-stream')
const zlib = require('zlib')
const url = require('url')
//request.debug = true
//相关配置
const APP_CONF = {
reverse_url: "http://www.baidu.com",
port: 8000,
cookie: "",
debug: true
}
app.use('/', reverse)
function reverse(req, res) {
debug({
path: req.originalUrl,
// url: APP_CONF.reverse_url + req.originalUrl,
url: req.originalUrl,
method: req.method
})
const cookie = APP_CONF.cookie
// 反向代理链接
const options = {
// url: APP_CONF.reverse_url + req.originalUrl,
url: req.originalUrl,
headers: {
//cookie: APP_CONF.cookie
}
}
let r = null
if (/POST|PUT/i.test(req.method)) { //假如是post/put请求
r = request.post(Object.assign(options, { json: req.body }))
.on('response', function (response) {
// 响应相应post/put请求
})
req.pipe(r).pipe(res)
} else {
let mime = ''
let isGzip = false
r = request(options)
.on('response', function (response) {
mime = response.headers['content-type']
if (response.headers['content-encoding'] == 'gzip') {
isGzip = true
}
res.writeHead(response.statusCode, response.headers)
})
// req 可读流 ==传递chunk==> r 可写流 ==返回目标流传递chunk==> concat 可写流
req.pipe(r).pipe(concat(function (body) {
// 拦截内容,根据mime去处理不同body
debug({
mime: mime,
divide: "========================="
})
if (mime && mime.indexOf("text/html") !== -1) {
if (isGzip) {
const decoded = zlib.gunzipSync(body)
// console.log(decoded)
// const html = decoded.toString().replace(/百度/g, '谷歌')
const html = decoded.toString()
const $ = cheerio.load(html)
// console.log($('.mnav').text('点我'))
// html相关处理
// cheerio
body = $.html()
res.end(zlib.gzipSync(body))
} else {
const $ = cheerio.load(decoded.toString())
// html相关处理
// cheerio
body = $.html()
res.end(body)
}
}
}))
}
}
function debug(obj) {
if (APP_CONF.debug) {
for (const i in obj) {
console.log(i + ":", obj[i])
}
}
}
const server = app.listen(APP_CONF.port, function () {
const host = server.address().address
const port = server.address().port
console.log('app listening at http://%s:%s', host, port)
})