Node.js 代理请求正文

发布于 2025-01-07 01:32:57 字数 592 浏览 0 评论 0 原文

我在使用 node.jsnode-http-proxy 获取双重代理请求的 response 正文时遇到一个简单的问题。

我基本上想做的就是在 Chrome 中将我的服务器的 IP 和端口设置为代理(可以工作),然后将请求代理到其他服务器。

我是这样做的:

var ip = {'xx.xx.xx.xx', 8080};

var proxy = new httpProxy.HttpProxy({ 
    target: {
        port : ip[0], host : ip[1]
    }
});

var server = http.createServer(function(req, res) {
    proxy.proxyRequest(req, res);
    proxy.on('data', function() { console.log(data);});
}).listen(8001)

不幸的是,没有一个“数据”事件对我有用......“结束”事件对我有用,但我从未设法获得尸体。有谁知道如何实现这一目标?我需要将每个请求的正文保存到特定文件中。

I am having a simple problem getting the response body of a double proxied request using node.js and node-http-proxy.

What I am basically trying to do, is to set the IP and port of my server in Chrome as a proxy (which works), which will then proxy the request to an other server.

Here is how I do it :

var ip = {'xx.xx.xx.xx', 8080};

var proxy = new httpProxy.HttpProxy({ 
    target: {
        port : ip[0], host : ip[1]
    }
});

var server = http.createServer(function(req, res) {
    proxy.proxyRequest(req, res);
    proxy.on('data', function() { console.log(data);});
}).listen(8001)

Unfortunately, none of the "on data" events are working for me here... The "end" events are, but I never managed to get the bodies. Does anyone know how to achieve this ? I need to save the body of each requests to a specific file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫之都 2025-01-14 01:32:57

是的……这超出了我的想象。请注意,我代理到端口 80,因为大多数网站都在端口 80 上提供服务。根据您的特定用例更改代码。这是在 CoffeeScript 中。

日志请求标头:

fs = require('fs')
httpProxy = require('http-proxy')

fsw = fs.createWriteStream('myfile.txt', flags: 'a', mode: 0666, encoding: 'utf8')
server = httpProxy.createServer  (req, res, proxy) ->
           req.connection.pipe(fsw) #runs in parallel with the proxy... don't you love Node.js?
           proxy.proxyRequest(req, res, {
             host: require('url').parse(req.url).hostname,
             port: 80
           })
server.listen(8080)

JS Translation

将“localhost”和端口 8080 作为您的浏览器代理。这对你有用吗?

记录请求正文:

fs = require('fs')
httpProxy = require('http-proxy')


server = httpProxy.createServer  (req, res, proxy) ->
           body = ''
           req.on 'data', (chunk) ->
             body += chunk

           req.on 'end', ->
             fs.writeFile('mybody.txt', body, 'utf8')

           proxy.proxyRequest(req, res, {
             host: require('url').parse(req.url).hostname,
             port: 80
           })
server.listen(8080)

我对此进行了测试,可以确认它记录了 POST/PUT 的正文。

记录响应正文:

fsw = fs.createWriteStream('myfile.txt', flags: 'a', mode: 0666, encoding: 'utf8')
server = httpProxy.createServer  (req, res, proxy) ->
  oldwrite = res.write
  res.write = (data, encoding, fd) ->
    fsw.write(data)
    res.write = oldwrite
    res.write(data, encoding, fd)
    res.write = oldwrite #<--- patch again before we leave the method


  proxy.proxyRequest(req, res, {
    host: require('url').parse(req.url).hostname,
    port: 80
  })

server.listen(8080)

可能不是最干净的方式,但我可以确认它有效。

Yes... this is off the top of my head. Note, that I'm proxying to port 80 since most websites serve on port 80. Change the code for your specific use case. This is in CoffeeScript.

Log Request Headers:

fs = require('fs')
httpProxy = require('http-proxy')

fsw = fs.createWriteStream('myfile.txt', flags: 'a', mode: 0666, encoding: 'utf8')
server = httpProxy.createServer  (req, res, proxy) ->
           req.connection.pipe(fsw) #runs in parallel with the proxy... don't you love Node.js?
           proxy.proxyRequest(req, res, {
             host: require('url').parse(req.url).hostname,
             port: 80
           })
server.listen(8080)

JS Translation

Put 'localhost' and port 8080 for your browser proxy. Does this work for you?

Log Request Body:

fs = require('fs')
httpProxy = require('http-proxy')


server = httpProxy.createServer  (req, res, proxy) ->
           body = ''
           req.on 'data', (chunk) ->
             body += chunk

           req.on 'end', ->
             fs.writeFile('mybody.txt', body, 'utf8')

           proxy.proxyRequest(req, res, {
             host: require('url').parse(req.url).hostname,
             port: 80
           })
server.listen(8080)

I tested this and can confirm that it logs the body of a POST/PUT.

Log Response Body:

fsw = fs.createWriteStream('myfile.txt', flags: 'a', mode: 0666, encoding: 'utf8')
server = httpProxy.createServer  (req, res, proxy) ->
  oldwrite = res.write
  res.write = (data, encoding, fd) ->
    fsw.write(data)
    res.write = oldwrite
    res.write(data, encoding, fd)
    res.write = oldwrite #<--- patch again before we leave the method


  proxy.proxyRequest(req, res, {
    host: require('url').parse(req.url).hostname,
    port: 80
  })

server.listen(8080)

Might not be the cleanest way, but I can confirm that it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文