1

I would like to capture an async call made by "request".
The call I am looking to intercept is "https://api.ap.org/v2/yada/yada" .

I want to intercept this third party call to api.ap.org and redirect it to another service, say 127.0.0.1:3001.

I would also like to add headers during this intercept process.

I know how to intercept all calls made by the express js route via http-proxy, but this does not intercept calls made within nodejs itself.

 router.get('/', function(req, res, next) {
   request("https://api.ap.org/v2/yada/yada", {}, (err, data) => {
       console.log('---- call made')
       console.log(data);
    });
   res.render('index', { title: 'Express' });
 });

UPDATE - from Estus

function patchedRequest(url, options, ...args) {
  let newUrl = 'https://www.google.com/' // replace url with another one;
  console.log('------ args');
  console.log(url);
  console.log(options);
  if(url.match(/api\.ap\.org/).length){
      options = {};
      newUrl = 'http://127.0.0.1:3000/api'
  }
  return originalRequest(newUrl, options, ...args);
}
  • This allows me to intercept the call to the third party API and send it the service of my choosing.

Thanks Estus!

5
  • this does not intercept calls made within nodejs itself - what does exactly not work? Is a callback not called? Sep 12, 2018 at 17:17
  • Long story short, I cannot change the code inside this app. I can only add an external app or add some code to the entry "app.js" file. There are thousands of calls made to third party endpoints. I need to redirect all of them but I am using api.ap.org as a starting point.
    – Wes Duff
    Sep 12, 2018 at 17:21
  • You can mock request, with rewire or something. Or proxy Node requests globally, stackoverflow.com/questions/18586902/… . Or specify a proxy just for request, github.com/request/… . The actual solution depends on how things are done in your case. Sep 12, 2018 at 17:28
  • "request" is required thousands of times. Each require is a new instance. I dont think "request" made their module into a singleton. I would have to fork the repo and add a hook into the "request" module and use my fork instead. I tried to use nginx as a NON transparent forward proxy and had issues with that as well.
    – Wes Duff
    Sep 12, 2018 at 17:40
  • request itself is a singleton. JS modules are singletons by definition. require('request') === require('request'). You can create a wrapper with proxyquire (I don't think that rewire does the job) or else instead of forking request. Sep 12, 2018 at 17:42

1 Answer 1

1

This can be done by mocking original request module.

This is roughly how cache-mangling libraries like proxyquire work:

patch-request.js

const originalRequest = require('request');

function patchedRequest(url, ...args) {
  const newUrl = 'https://www.google.com/' // replace url with another one;
  return originalRequest(newUrl, ...args);
}

Object.assign(patchedRequest, originalRequest);

for (const verb of 'get,head,options,post,put,patch,del,delete'.split(',')) {
  patchedRequest[verb] = function (url, ...args) {
    const newUrl = 'https://www.google.com/' // replace url with another one;
    return originalRequest[verb](newUrl, ...args);
  };
}

module.exports = require.cache[require.resolve('request')].exports = patchedRequest;

index.js

// patch request before it's required anywhere else
require('./patch-request');

// load the app that uses request
6
  • I think your the man! I tried many different angles and this is the easiest solution. - Updated question with your answer
    – Wes Duff
    Sep 12, 2018 at 18:40
  • Nice work... Also this is just for a test harness I am creating. It will not be released to the public or need of securities.
    – Wes Duff
    Sep 12, 2018 at 18:44
  • You're welcome. A proxy server would be probably more flexible and transparent solution, though it requires more things to care about. As for testing, a less hacky way would be to replace require('request') with rewire('request') in the app if possible, this is what rewire is for. Sep 12, 2018 at 18:45
  • True, I was given till the end of today to fix this, then I am off to other less green pastures haha. I have a tech debt story to come back and come up with a cleaner solution. Thanks again for your help
    – Wes Duff
    Sep 12, 2018 at 19:20
  • I see. The solution in the answer is ok as regular solution and perfectly ok as quick fix, I guess this fits your needs. Sep 12, 2018 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.