1

I'm still new still newish to node, and having some troubling figuring out how to set a new response body with the http-proxy-middleware package.

That is, I have the following small program that proxies requests

var express = require('express');
var proxy = require('http-proxy-middleware');

var app = express();

app.use('**', proxy({
    target: 'http://alanstorm.com', 
    changeOrigin: true,
    onProxyRes:function (proxyRes, req, res) {

        //I want to do something here to change the response
        console.log("Called");
        console.log(proxyRes);
        console.log(res);
    }
}));
app.listen(3000);

i.e. -- I can make a request to http://localhost:3000, and it will proxy the request to my personal website.

I've also successfully setup a response listener (onProxyRes) above. I seem to have access to a proxy response object, a request object, and a response object. What I'd like to do is, in the onProxyRes method, is change the response if certain things are true or not.

However, it's not clear how to do this with the proxyRes or res objects, and I'm not sure how to look up the available methods on these objects. I tried console.loging them for useful properties, and found nothing useful.

If someone know how i can modify the bodies of the response objects, that would be great. If someone can tell me how I can figure out what methods exist on those objects, that would be extra great.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • 1
    Using a debugger would allow you to explore the objects, see https://medium.com/@paul_irish/debugging-node-js-nightlies-with-chrome-devtools-7c4a1b95ae27 – skirtle Oct 24 '17 at 18:43
  • @skirtle woah, neat! Thanks for the tip. What moden times we live in. – Alana Storm Oct 24 '17 at 18:47

0 Answers0