[英]why req.body is undefined in 'proxyRes'
Below is my code 下面是我的代码
import { createProxyServer } from 'http-proxy';
import BodyParser from 'body-parser';
const jsonParser = BodyParser.json();
const proxy = createProxyServer({
target: 'http://127.0.0.1:7800',
proxyTimeout: 1000,
});
app.use('/api', (req, res) => {
proxy.web(req, res);
});
proxy.on('proxyRes', (proxyRes, req, res) => {
proxyRes.on('data', (dataBuffer) => {
console.log('req.body ', req.body);
const data = dataBuffer.toString('utf8');
console.log(data);
});
});
I am getting req.body as undefined 我得到req.body为未定义
If I use middleware jsonParser
in app.post(..)
then this routes hangs and I get timeout error in front end. 如果我在
app.post(..)
使用中间件jsonParser
,则此路由会挂起,并且前端会出现超时错误。 code is below. 代码如下。
app.use('/api', jsonParser, (req, res) => {
proxy.web(req, res);
});
I want to make logs of response & request when res.statusCode !== 200
我想在
res.statusCode !== 200
时记录响应和请求的日志
After some clean up of the code (remove }
extra, remove duplicate line) I was able to get some runnable code, not sure if this is what you are looking for. 经过一些代码清理(删除
}
,删除重复的行),我能够得到一些可运行的代码,不确定这是否是您想要的。 I hope this help. 希望对您有所帮助。
const proxy = createProxyServer({
target: 'http://127.0.0.1:7800',
proxyTimeout: 1000,
});
app.use(BodyParser())
app.use('/api', (req, res) => {
proxy.on('proxyRes', (proxyRes) => {
proxyRes.on('data', (dataBuffer) => {
console.log('req.body ', req.body);
const data = dataBuffer.toString('utf8');
console.log(data);
});
});
proxy.web(req, res);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.