question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ Question ] How do you manipulate proxy response

See original GitHub issue

Hi, I’m using Express 4,

In normal way, I can do the following

    app.get('/v1/users/:username', function(request, response, next) {
        var username = request.params.username;
        findUserByUsername(username, function(error, user) {
            if (error) return next(error);
            return response.render('user', user);
        });
    });

But how do I execute custom logic if I’m using proxy, let’s say I want to manipulate some of the data before response to the user? Is there a good pattern to do that with this middleware ?

app.use('/api', proxy({target: 'http://www.example.org', changeOrigin: true}));

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:38 (2 by maintainers)

github_iconTop GitHub Comments

57reactions
cokeknightcommented, Mar 15, 2017

here is my answer,

onProxyRes :function(proxyRes, req, res){
      var _write = res.write;
      var output;
      var body = "";
      proxyRes.on('data', function(data) {
          data = data.toString('utf-8');
          body += data;
      });
      res.write = function (data) {
        try{
          eval("output="+body)
          output = mock.mock(output)
          _write.call(res,JSON.stringify(output));
        } catch (err) {}
      }
    }

add onProxyRes option on the http-proxy-middleware use the data event on the proxyRes to get the output then modify the output in res.write

33reactions
mrt123commented, Sep 20, 2018

My solution:

const zlib = require('zlib');
...
onProxyRes: (proxyRes, req, res) => {
	let originalBody = new Buffer('');
	proxyRes.on('data', function (data) {
		originalBody = Buffer.concat([originalBody, data]);
	});
	proxyRes.on('end', function () {
		const bodyString = zlib.gunzipSync(originalBody).toString('utf8')
		const objectToModify = JSON.parse(bodyString)
		objectToModify.modification = 'Mickey Mouse'
		res.end(JSON.stringify(objectToModify));
	});
},
selfHandleResponse: true . // necessary to avoid res.end being called automatically
...
Read more comments on GitHub >

github_iconTop Results From Across the Web

[ Question ] How do you manipulate proxy response #528
But how do I execute custom logic if I'm using proxy, let's say I want to manipulate some of the data before response...
Read more >
node.js - How do you manipulate proxy response
I think this is the correct way to do it according to the official documentation of http-proxy. modify -response app.use('/api', proxy({ ...
Read more >
How to Intercept Requests & Modify Responses With Burp Suite
It is a proxy through which you can direct all requests, and receive all responses, so that you can inspect and interrogate them...
Read more >
Mocking And Manipulating API Behavior With A Local Proxy ...
We want the API to respond with unusual responses like different HTTP status codes or specially crafted but valid values. The proxy should...
Read more >
Membrane Service Proxy FAQ
Q : Can Membrane manipulate intercepted request and response message? A: Membrane is made to be used for such tasks as intercepting and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found