webpack 配置代理请求正文日志记录
webpack config proxy request body logging
此处显示的 Webpack 开发服务器代理配置文档:
https://webpack.js.org/configuration/dev-server/#devserver-proxy
说它使用 http-proxy-middleware:
https://github.com/chimurai/http-proxy-middleware#http-proxy-events
使用上面记录的 onProxyRes
函数 link 我执行以下操作:
function onProxyRes(proxyRes, req, res) {
proxyRes.headers['x-added'] = 'foobar'; // add new header to response
delete proxyRes.headers['x-removed']; // remove header from response
console.log(req.headers) // log headers
console.log(req.body) // undefined
console.log(proxyReq.body) // undefined
}
我的问题,虽然其他一切都很好 - 我无法记录请求正文 - 它 returns undefined
有人知道如何读取请求正文以进行调试吗?我是否需要以某种方式使用 npm body-parser
模块?如果是这样,如何?谢谢
我尝试使用 express body-parser module but it caused the requests to hang. Using the body 模块记录请求以记录请求正文修复它。
const anyBody = require('body/any')
onProxyReq(proxyReq, req, res) {
anyBody(req, res, function (err, body) {
if (err) console.error(err)
console.log(body)
})
})
请注意,我在 express 中也使用了相同的方法,如下所示:
app.use((req, res, next) => {
anyBody(req, res, function (err, body) {
if (err) console.error(err)
console.log(body)
})
next()
})
我使用 body-parser
解决了类似的问题。
我试图在将请求 body 发送到服务器之前对其进行修改,这导致请求挂起,这可能是因为更改了请求的 body、Content-Length
不再匹配(导致它被截断)。
解决方案是 "resizing" Content-Length
在写入新请求之前 body:
var bodyParser = require('body-parser');
devServer: {
before: function (app, server) {
app.use(bodyParser.json());
},
onProxyReq: function (proxyReq, req, res) {
req.body = {
...req.body,
myProperty: "myPropertyValue"
};
const body = JSON.stringify(req.body);
proxyReq.setHeader('Content-Length', Buffer.byteLength(body));
proxyReq.write(body);
}
}
不确定,但在您的情况下,可能是请求已被 adding/removing headers 截断,导致它挂起。
此处显示的 Webpack 开发服务器代理配置文档:
https://webpack.js.org/configuration/dev-server/#devserver-proxy
说它使用 http-proxy-middleware:
https://github.com/chimurai/http-proxy-middleware#http-proxy-events
使用上面记录的 onProxyRes
函数 link 我执行以下操作:
function onProxyRes(proxyRes, req, res) {
proxyRes.headers['x-added'] = 'foobar'; // add new header to response
delete proxyRes.headers['x-removed']; // remove header from response
console.log(req.headers) // log headers
console.log(req.body) // undefined
console.log(proxyReq.body) // undefined
}
我的问题,虽然其他一切都很好 - 我无法记录请求正文 - 它 returns undefined
有人知道如何读取请求正文以进行调试吗?我是否需要以某种方式使用 npm body-parser
模块?如果是这样,如何?谢谢
我尝试使用 express body-parser module but it caused the requests to hang. Using the body 模块记录请求以记录请求正文修复它。
const anyBody = require('body/any')
onProxyReq(proxyReq, req, res) {
anyBody(req, res, function (err, body) {
if (err) console.error(err)
console.log(body)
})
})
请注意,我在 express 中也使用了相同的方法,如下所示:
app.use((req, res, next) => {
anyBody(req, res, function (err, body) {
if (err) console.error(err)
console.log(body)
})
next()
})
我使用 body-parser
解决了类似的问题。
我试图在将请求 body 发送到服务器之前对其进行修改,这导致请求挂起,这可能是因为更改了请求的 body、Content-Length
不再匹配(导致它被截断)。
解决方案是 "resizing" Content-Length
在写入新请求之前 body:
var bodyParser = require('body-parser');
devServer: {
before: function (app, server) {
app.use(bodyParser.json());
},
onProxyReq: function (proxyReq, req, res) {
req.body = {
...req.body,
myProperty: "myPropertyValue"
};
const body = JSON.stringify(req.body);
proxyReq.setHeader('Content-Length', Buffer.byteLength(body));
proxyReq.write(body);
}
}
不确定,但在您的情况下,可能是请求已被 adding/removing headers 截断,导致它挂起。