Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i pass my proxy settings to node-geocoder? #315

Open
fahadsubzwari924 opened this issue Mar 24, 2021 · 2 comments
Open

How can i pass my proxy settings to node-geocoder? #315

fahadsubzwari924 opened this issue Mar 24, 2021 · 2 comments

Comments

@fahadsubzwari924
Copy link

fahadsubzwari924 commented Mar 24, 2021

I have my node.js server running on a vm along with a proxy on it. Now i am using node-geocoder to get lat lng from node-geocoder like this

 const NodeGeocoder = require('node-geocoder');
 var geocoder = NodeGeocoder(constants.config.geocoderOptions);


const geoCodeAddress = await geocoder.geocode(address.location);

now it's throwing an error due to proxy like this

request to https://maps.googleapis.com/maps/api/geocode/json?sensor=false&key=my-key&address=Korangi%20Creek failed, reason: getaddrinfo ENOTFOUND maps.googleapis.com maps.googleapis.com:443

So how can i pass my proxy settings in node-geocoder ?

@pdavies011010
Copy link

pdavies011010 commented Apr 28, 2022

The README shows how to override the fetch method:


// Set specific http request headers:
const nodeFetch = require('node-fetch');

const geocoder = NodeGeocoder({
  provider: 'google',
  fetch: function fetch(url, options) {
    return nodeFetch(url, {
      ...options,
      headers: {
        'user-agent': 'My application <email@domain.com>',
        'X-Specific-Header': 'Specific value'
      }
    });
  }
});

In there you can add any options you want, including theoretically overriding the agent using something like the http-proxy-agent module:

const nodeFetch = require('node-fetch');
const HttpProxyAgent = require('http-proxy-agent');
const options = {
    provider: 'openstreetmap',
    fetch: function (url, options) {
      // Configure to use the proxy from HTTP_PROXY or http_proxy env variable
      let proxy = process.env.http_proxy || process.env.HTTP_PROXY;
      if (proxy) {
        options.agent = new HttpProxyAgent(proxy);
      }

      return nodeFetch(url, {
        ...options,
      });
    },
  };

@pdavies011010
Copy link

Also note that node-geocoder uses node-fetch version 2, and you will need to use v2 as well in the above example. I have tried using it with node-fetch v3 but it had problems (caused my unit tests to hang indefinitely, not sure what the underlying issue actually was). I will open an issue about it and if possible will create a PR to upgrade to node-fetch v3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants