admin管理员组文章数量:1402217
In our project we're using the "http-proxy-middleware"() npm package for proxy.
There is the "onProxyRes" function to subscribe to http-proxy's event.
And there is an example of that function:
function onProxyRes(proxyRes, req, res) {
proxyRes.headers['x-added'] = 'foobar' // add new header to response
delete proxyRes.headers['x-removed'] // remove header from response
}
I'm just interesting is it possible somehow based on proxyRes write changed response in res object and do not copy data directly from proxyRes object?
Just example:
proxyRes(readable stream contains the following data: {"url": "http://domain/test"}, I'd like to modify that response and have res with data like that: {{"url": "http://changedDomain/test"}} and do not copy data from proxyRes directly
In our project we're using the "http-proxy-middleware"(https://www.npmjs./package/http-proxy-middleware) npm package for proxy.
There is the "onProxyRes" function to subscribe to http-proxy's event.
And there is an example of that function:
function onProxyRes(proxyRes, req, res) {
proxyRes.headers['x-added'] = 'foobar' // add new header to response
delete proxyRes.headers['x-removed'] // remove header from response
}
I'm just interesting is it possible somehow based on proxyRes write changed response in res object and do not copy data directly from proxyRes object?
Just example:
proxyRes(readable stream contains the following data: {"url": "http://domain/test"}, I'd like to modify that response and have res with data like that: {{"url": "http://changedDomain/test"}} and do not copy data from proxyRes directly
Share Improve this question edited Jan 2, 2019 at 11:21 Eugene Rotar asked Jan 2, 2019 at 11:16 Eugene RotarEugene Rotar 931 gold badge2 silver badges9 bronze badges2 Answers
Reset to default 2Maybe it looks ugly little bit, but I'm able to manage that with the following code:
function onProxyRes(proxyResponse, request, serverResponse) {
var body = "";
var _write = serverResponse.write;
proxyResponse.on('data', function (chunk) {
body += chunk;
});
serverResponse.write = function (data) {
try{
var jsonData = JSON.parse(data);
// here we can modify jsonData
var buf = Buffer.from(JSON.stringify(jsonData), 'utf-8');
_write.call(serverResponse,buf);
} catch (err) {
console.log(err);
}
}
}
I don't think it's necessary to copy data to res
as proxyRes
already has changedDomain
.
Here is the set up I implemented:
const express = require('express');
const httpProxy = require('http-proxy-middleware');
const app = express();
app.use('/api', httpProxy({ target : 'sometarget.', changeOrigin : true, onProxyRes})
function onProxyRes (proxyResponse, request, response) {
console.log('proxyResponse', proxyResponse.headers);
console.log('response', response.headers);
}
// Results
/*
proxyResponse { date: 'Wed, 02 Jan 2019 12:06:40 GMT',
server: 'Apache',
location: 'http://sometarget./api',
'cache-control': 'max-age=30',
expires: 'Wed, 02 Jan 2019 12:07:10 GMT',
'content-length': '231',
connection: 'close',
'content-type': 'text/html; charset=iso-8859-1' }
response undefined
*/
Everything you'll need will be in proxyRes
unless you have a specific use for response
...
本文标签: javascriptCustom response with httpproxymiddleware packageStack Overflow
版权声明:本文标题:javascript - Custom response with http-proxy-middleware package - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744260549a2597694.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论