node-http-proxy如何匹配响应请求

时间:2018-10-30 07:53:31

标签: javascript node.js http proxy http-proxy

我想创建一个类似于Telerik的Fiddler的UI。为此,我创建了一个具有node-http-proxy的代理,该代理工作得很完美:-)

但是现在我遇到了如何使响应与请求匹配的问题,而且我完全不知道我可以尝试什么。

这是我当前的代码:

filter

有人知道我如何将let httpProxy = require('http-proxy'); let connect = require('connect'); let bodyParser = require('body-parser'); let http = require('http'); let proxy = httpProxy.createProxyServer(); let app = connect() .use(bodyParser.json()) //json parser .use((req, res) => { let options = { target: "myTargetHere" }; proxy.web(req, res, options); }); http.createServer(app).listen(8000); proxy.on("proxyReq", (proxyReq, req, res) => { if (req.body) { let bodyData = JSON.stringify(req.body); let bodyLength = Buffer.byteLength(bodyData) proxyReq.setHeader("Content-Length", bodyLength); proxyReq.write(bodyData); } }); proxy.on('proxyRes', (proxyRes, req, res) => { let body = new Buffer.alloc(0); let bodyLength = 0; proxyRes.on('data' , data => { process.stdout.write("c "); body = Buffer.concat([body, data]); }); proxyRes.on("end", () => { bodyLength = Buffer.byteLength(body); body = body.toString(); console.log("response body length: ", bodyLength); }); }); 中的响应与我在proxy.on('proxyRes', ...)中的请求相匹配吗?每个请求/响应中是否都有独特之处?

0 个答案:

没有答案