Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure node-fetch to use the company proxy?

I am executing a standalone nodejs script (no web server involved) that needs to fetch result from a third party api. The program uses 'node-fetch' to do the fetch(url) and I run it using node .\test.js from command line.

It fails when I am connected to our company network but works fine on direct internet. I have configured the proxy settings in npm and could see that npm config ls shows the correct values for proxy and https-proxy.

So the questions are: 1. Does running the test.js via node not pick the proxy config from npm? 2. How to make sure that the fetch(url) call goes through our proxy?

Thanks in advance

like image 994
Ashok Avatar asked Feb 11 '20 05:02

Ashok


People also ask

How do I use a node-fetch proxy?

// proxy_test.py // npm install node-fetch // npm install https-proxy-agent const fetch = require('node-fetch'); const HttpsProxyAgent = require('https-proxy-agent'); (async () => { const proxyAgent = new HttpsProxyAgent('http://46.250.171.31:8080'); const response = await fetch('https://httpbin.org/ip?json', { agent: ...

Can you use the fetch API in node?

Fetch() support is now available in Node. js as an experimental core feature. Fetch() is a well-liked cross-platform HTTP client API that functions in browsers and Web/Service Workers.

Does node-fetch follow redirects?

Normally, fetch transparently follows HTTP-redirects, like 301, 302 etc.


1 Answers

This worked for me, try using this : https://github.com/TooTallNate/node-http-proxy-agent

The request formed will be similar to this:

fetch('accessUrl', {agent: new HttpsProxyAgent('proxyHost:proxyPort')})
    .then(function (res) {
    })
like image 178
Abinash Avatar answered Oct 14 '22 19:10

Abinash