-
-
Notifications
You must be signed in to change notification settings - Fork 16.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to recover the modification of Middleware #4157
Comments
Hi @wll8, the approach you show above will not work as you expect. There are a few issues:
Can I ask you why you want to "recover" the raw body? If you have a really good reason which cannot be done in another way, here is one approach to this problem: server.use((req, res, next) => {
const rawBody = req.body
bodyParser.json(req, res, (err) => {
req.body = rawBody
next(err)
})
}) |
Thank you. @wesleytodd
My original idea was: // Some requests are forwarded to the target server, which requires the original request
server.use(proxy('/api', {
target: 'http://www.example.org',
onProxyReq: (proxyReq, req, res) => {
// I want to parse the `req` through middleware here, the purpose is to make it easy for me to get what is forwarded
// But do not want to change the content of the `req`. Because the target server needs the original `req` content.
},
}))
// For requests that do not need to be forwarded, use middleware to easily obtain the content of the request
server.use(middlewares)
server.post('/file/upload', () => {}) I tried the following scenarios: The target server needs the original requestserver.use([...middlewares, (req, res, next) => {
req.body // The `middlewares` are used to easily obtain the `body`
// But unfortunately, because `req` was parsed by the middleware, the proxy target server does not accept this request.
next()
}])
server.use(proxy()) Try using server2server.use('*', server2)
server2.use([...middlewares, (req, res, next) => {
req.body // I thought the new `server2` would no longer affect` server`
next()
}])
server.use(proxy()) // Unfortunately, In fact, the `req` obtained here is the content processed by middlewares middleware Try to copy the original requestlet REQ_RAW = {}
server.use((req, res, next) => {
REQ_RAW = cloneDeep(req)
next()
})
server.use(middlewares) // I want to get the parsed content here, e.g. bodyParser
server.use((req, res, next) => {
req = REQ_RAW
next()
})
server.use(proxy()) // Unfortunately, In fact, the `req` obtained here is the content processed by middlewares middleware On gitter, no one replied to my questions T_T.
I worry that some middleware does not provide callbacks. |
If I understand correctly, your issue is that using the typical I found some issues on other repos where others have had similar issues (working with And finally here is an implementation which re-streams the request body to the proxy after it has been parsed by |
Thank you. I found a simple solution server.use((req, res, next) => {
// Method 1
// middlewaresObj from jsonServer.defaults
middlewaresObj.jsonParser(req, res, () => {
console.log('req.body', req.body)
})
next()
// // Method 2
// const bodyParser = require('body-parser')
// bodyParser.json({
// limit: '10mb',
// extended: false
// })(req, res, () => {
// console.log('req.body', req.body)
// })
// next()
})
server.use(proxy()) If you need more methods, you can refer to these links:
|
How to recover the modification of Middleware ?
I'm using the proxy function, but before the proxy, I want to use middleware to get the content in the body easily
The text was updated successfully, but these errors were encountered: