-
Notifications
You must be signed in to change notification settings - Fork 244
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
body parser on server middleware breaks POST request body on axios proxified requests #145
Comments
Same issue here. After remove the body parser on server, now proxy request is working. |
Unfortunately, there is nothing to do with this module. But generally, it is much better to run API server separately from nuxt and instead, proxy |
It took a while to find the reason of timeouts that this is a problem of body-parser + proxy. I resolved this by adding a serverMiddleware after the bodyParser like this:
|
I fixed, using workaround from official github |
Where did you put the code Many Thanks |
@mulhoon put following configuration in your ...
axios: {
proxy: true
},
proxy: {
'/api/': {
target: process.env.URL,
pathRewrite: { '^/api/': '/' },
changeOrigin: true,
onProxyReq: function log (proxyReq, req, res) {
// console.log(req.body)
// console.log(proxyReq.getHeader('Content-Type'))
if (!req.body || !Object.keys(req.body).length) {
return
}
const contentType = proxyReq.getHeader('Content-Type')
const writeBody = (bodyData) => {
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData))
proxyReq.write(bodyData)
}
if (contentType.includes('application/json') || contentType.includes('application/x-www-form-urlencoded')) {
writeBody(JSON.stringify(req.body))
}
}
}
},
... |
Thank you @alexanderniebuhr 🙏🏻 |
@alexanderniebuhr 's code Solved my problem, in case others searching for this : Vue ( Element Admin ) + webpack dev server + axios POST request with json body, If you're confronted with error message like " Just paste my configuration for your information: devServer: {
port: port,
open: true,
overlay: {
warnings: false,
errors: true
},
proxy: {
// change xxx-api/login => mock/login
// detail: https://cli.vuejs.org/config/#devserver-proxy
[process.env.VUE_APP_BASE_API]: {
target: `http://example.com:8084`,
changeOrigin: true,
logLevel: 'debug',
pathRewrite: {
['^' + process.env.VUE_APP_BASE_API + '/some-api']: ''
},
headers: {
'Connection': 'keep-alive'
},
onProxyReq: function log(proxyReq, req, res) {
// console.log(req.body)
// console.log(proxyReq.getHeader('Content-Type'))
if (!req.body || !Object.keys(req.body).length) {
return
}
const contentType = proxyReq.getHeader('Content-Type')
const writeBody = (bodyData) => {
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData))
proxyReq.write(bodyData)
}
if (contentType.includes('application/json') || contentType.includes('application/x-www-form-urlencoded')) {
writeBody(JSON.stringify(req.body))
}
}
},
},
before: require('./mock/mock-server.js')
}, |
Version
v5.1.0
Reproduction link
https://jsfiddle.net/8Ls7tafj/7/
Steps to reproduce
the steps to reproduce are in the fiddle, wich is just a nuxt.config.js exemple
What is expected ?
unaltered request body
What is actually happening?
request body is altered by body parser
Additional comments?
for people who experience the same issue, you can use this as a workaround
chimurai/http-proxy-middleware#40 (comment)
The text was updated successfully, but these errors were encountered: