【发布时间】:2025-12-04 04:20:07
【问题描述】:
我需要能够在我的请求代理应用程序中使用 http 请求正文,然后在实际的 Web 服务中再次使用。我正在使用 restreamer 来“重置”流(甚至自己编写了中间件,没有任何改变)。 Web 服务可以很好地接收正文,但由于从不发出 end,我无法继续请求。
使用邮递员进行测试,发送原始正文,设置内容类型。任何建议将不胜感激。谢谢!
var express = require('express')
, bodyParser = require('body-parser')
, http = require('http')
, restreamer = require('connect-restreamer')
, httpProxy = require('http-proxy')
var app = express();
app.use(function (req, res, next) {
var body = '';
req.on('data', function (chunk) {
body += chunk.toString('utf8');
});
req.on('end', function (chunk) {
req.body = JSON.parse(body)
next();
});
});
app.use(restreamer());
var proxy = httpProxy.createServer();
app.all('*', function (req, res) {
proxy.web(req, res, {
target: 'http://localhost:8001'
});
});
http.createServer(app).listen(8000);
app2 = express();
app2.use(function (req, res, next) {
var body = '';
req.on('data', function (chunk) {
body += chunk.toString('utf8');
});
req.on('end', function (chunk) {
req.body = JSON.parse(body)
next();
});
});
app2.all('*', function (req, res) {
res.send(req.body)
});
http.createServer(app2).listen(8001);
【问题讨论】:
-
也用 curl 测试过:
curl -X POST -d @data.json http://localhost:8000 --header "Content-Type:application/json"
标签: node.js express node-http-proxy