1

i want to console log success and error response of this proxy request, however i'm not able to find any option to do that:

app.get(
"/api/:resource/:id",
requestProxy({
cache: redis.createClient(),
cacheMaxAge: 60,
url: "https://someapi.com/api/:resource/:id",
query: {
secret_key: process.env.SOMEAPI_SECRET_KEY
},
headers: {
"X-Custom-Header": process.env.SOMEAPI_CUSTOM_HEADER
}
})
);```

1 Answers1

1

You can use userResDecorator in option. Like this example:

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

app.use('/proxy', proxy('www.google.com', {
    userResDecorator: function(proxyRes, proxyResData, userReq, userRes) {
      console.log("Status Code",proxyRes.statusCode)
      console.log("Response Body",proxyResData.toString())
      return "OK"; // it should return string
    }
  }));
app.listen(8080, () => {})

Output Log

Demo

Documentation : https://www.npmjs.com/package/express-http-proxy#user-content-userresdecorator-was-intercept-supports-promise

  • Great answer! In my experience doing "return proxyResData" at the end of userResDecorator works best. Then errors propagate the way I expect them to. – Martin Omander Mar 23 '23 at 17:45