How can i pass my proxy settings to node-geocoder?
fahadsubzwari924 opened this issue · 2 comments
I have my node.js
server running on a vm
along with a proxy
on it. Now i am using node-geocode
r 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
?
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,
});
},
};
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.