gpt4 book ai didi

javascript - 使用 Node.js 代理 POST 并修改 header

转载 作者:行者123 更新时间:2023-12-03 07:02:18 27 4
gpt4 key购买 nike

我的本​​地开发脚本使用代理来绕过 CORS(除其他外)。我首先获取一些虚假的 cookie,这些 cookie 允许我在开发时跳过应用程序中的身份验证。然后,对 spoofCookies 的回调设置我的静态代理 - 它提供我所有的本地静态内容,然后为路由到/microsvc 的所有调用设置代理。我接听该电话,注入(inject)我的欺骗性 cookie,并将其转发到远程主机上的同一路径。

这个脚本非常适合 GET,但我不知道如何编写 POST 部分。请求从未显示我通过代理发送的 POST 表单,因此我一定做错了什么。

感谢您的宝贵时间

    /*jslint node:true white:true nomen:true es5:true*/

'use strict';

var PORT_HTTP = process.env.PORT || 4400;
var localAddress = "http://localhost:" + PORT_HTTP;

var apiAddress = { label: "On-site Ethernet Plug",
protocol: "http://",
port: 80,
host : "10.17.100.11",
microsvc: "/microsvc",
auth: "/login.form"
};

var https = require('https');
var path = require('path');
var express = require('express');
var app = express();
var spoofCookie = require('./utils/spoof-cookies');
var cookies = [];

var proxy = function(myPath, searchOptions, req, res, cookies) {
var options = {
hostname: apiAddress.host,
port: apiAddress.port,
path: apiAddress.microsvc + searchOptions,
rejectUnauthorized: false
};

if(cookies.length){
options.headers = {
"Cookie":cookies[0]
};
}

try {
console.log('FROM: ' + localAddress + myPath + searchOptions + '\n' +
'↳ TO: ' + apiAddress.protocol + apiAddress.host + apiAddress.microsvc + searchOptions);

options.method = req.method;

if(options.method == 'POST' || options.method == 'PUT') {

// HELP
// form data never appears in output
console.log(req);

} else {
var r = https.request(options, function(resX) {
resX.setEncoding('utf8');
var responseData = '';
resX.on('data', function(chunk) {
responseData += chunk;
});
resX.on('end', function() {
res.send(JSON.parse(responseData));
});
});

r.on('error', function(e) {
if (e.code == "ENOTFOUND") {
console.error("File not found on remote");
} else {
console.error('ff ' + e);
}
});
}

req.pipe(r).pipe(res);

} catch (e) {
console.error(e);
}
};

// START - first get spoofed cookies
spoofCookie.getCookies(apiAddress, function(cookies) {

// set up server
var httpServer = http.createServer(app);
httpServer.listen(PORT_HTTP);

// microservice proxy
app.use('/microsvc', function (req, res) {
proxy('/microsvc', req.url, req, res, cookies);
});

console.log("HTTP server started: " + localAddress);

});


// static server runs anything that isn't set up for proxy
app.use(express.static(path.resolve(path.join(__dirname, "..", "app"))));

最佳答案

express is not parsing默认请求正文。

req.body contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser and multer.

因此,body-parser 的设置看起来像这样。

首先,在声明路由之前将中间件附加到某处:

var bodyParser = require('body-parser');

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));

// parse application/json
app.use(bodyParser.json());

// microservice proxy
app.use('/microsvc', function (req, res) {
proxy('/microsvc', req.url, req, res, cookies);
});

然后,访问request.body中的请求内容:

if(options.method == 'POST' || options.method == 'PUT') {
console.log(req.body);
}

关于javascript - 使用 Node.js 代理 POST 并修改 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36985689/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com