node.js - 如何检查通过 node.js 代理的请求响应

标签 node.js

我正在尝试使用 node.js 来设置一个简单的代理服务器。其背后的想法是让对一个网络服务的所有网络服务调用都通过 node.js 代理,以便轻松检查和调试网络服务调用。

为此,我尝试使用以下代码来代理请求:

var
    url = require('url'),
    http = require('http'),
    acceptor = http.createServer().listen(8008);

acceptor.on('request', function(request, response) {
    console.log('request ' + request.url);
    request.pause();
    var options = url.parse(request.url);
    options.headers = request.headers;
    options.method = request.method;
    options.agent = false;

    var connector = http.request(options, function(serverResponse) {
            serverResponse.pause();
            response.writeHeader(serverResponse.statusCode, serverResponse.headers);
            serverResponse.pipe(response);
            serverResponse.resume();
    });
    request.pipe(connector);
    request.resume();
});

但我不知道在哪里检查/转储以归档响应。使用 Node 检查器,我正在查看行中的响应对象:serverResponse.pipe(response);但响应正文尚不可用。

我发现了以下问题 node.js proxied request body但它是用 CoffeeScript 编写的。

最佳答案

想法是编写您自己的'data' 处理程序,不要使用pipe()
一旦通过管道传输流,您就无法窃听数据。

关于node.js - 如何检查通过 node.js 代理的请求响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16972064/

相关文章:

node.js - 通过 Node.js 连接到远程 Docker

node.js - 无法运行 ionic 。收到 "No command ' ionic' found"

node.js - 使用StrongLoop的 "slc run"和 "node app.js"有什么区别

node.js - 在 Node JS 集群之间划分快速路由

javascript - 函数内部的 stub 函数调用

javascript - Node JS 无法读取未定义的属性 'length'

python - 使用node和python读取同一文件时结果的差异

mysql - 如何在 ExpressJs 上加载静态图像

node.js - Node + express : Multiple authentication strategies according to page and user type

node.js - nodejs emailjs 连接 econnrefused, smtp