简体   繁体   English

如何使用http-proxy获取请求的正文?

[英]How to get request's body using http-proxy?

I want to write a simple proxy which will save body of specific POST request to file. 我想编写一个简单的代理,它将特定POST请求的正文保存到文件中。

I tried http-proxy but the problem is i do not know how to get request's body. 我尝试了http-proxy,但问题是我不知道如何获取请求的正文。 There are headers and some other info, but no body in req object. 有标头和其他一些信息,但req对象中没有正文。 Here is what i have at the moment. 这是我目前所拥有的。 I inspected the logfile i create and do not see request body there. 我检查了我创建的日志文件,但没有看到请求正文。

var fs = require('fs'),
    http = require('http'),
    httpProxy = require('http-proxy');

var proxy = httpProxy.createProxyServer({});

var logger = function () {

    return function (request, response) {
        var logFile = fs.createWriteStream('./req' + new Date().getTime() + '.log');
        proxy.web(request, response, {target: 'http://localhost:8081'});

        var cache = [];
        logFile.write(
            JSON.stringify(request, function (key, value) {
                if (typeof value === 'object' && value !== null) {
                    if (cache.indexOf(value) !== -1) {
                        // Circular reference found, discard key
                        return;
                    }
                    // Store value in our collection
                    cache.push(value);
                }
                return value;
            }, 2)
        );
    }
};

var server = http.createServer(logger());
server.listen(8080);

Usually when I want to access request body with node I use the body-parser module. 通常,当我想使用节点访问请求正文时,我使用body-parser模块。

How to use it ! 如何使用它 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM