Anyway to set proxy setting in passportjs? Anyway to set proxy setting in passportjs? express express

Anyway to set proxy setting in passportjs?


Node.js doesn't use the http_proxy and https_proxy variables by default.

You have to tweak the agent parameter for the request but since you don't have control on that library you can change globally like this:

npm i tunnel --save

create a setup_proxy.js:

var env = process.env;if (!env['http_proxy']) return;var localUrls = [  'http://some-internal-url.mycompany.local',];var url    = require('url');var tunnel = require('tunnel');var proxy = url.parse(env['http_proxy']);var tunnelingAgent = tunnel.httpsOverHttp({  proxy: {    host: proxy.hostname,    port: proxy.port  }});var https = require('https');var http = require('http');var oldhttpsreq = https.request;https.request = function (options, callback) {  if (localUrls.some(function (u) {    return ~u.indexOf(options.host);  })){    return oldhttpsreq.apply(https, arguments);  }  options.agent = tunnelingAgent;  return oldhttpsreq.call(null, options, callback);};var oldhttpreq = http.request;http.request = function (options, callback) {  if (localUrls.some(function (u) {    return ~u.indexOf(options.host);  })){    return oldhttpreq.apply(http, arguments);  }  options.agent = tunnelingAgent;  return oldhttpreq.call(null, options, callback);};

require this at the very beginning require('./setup_proxy').

Notice that this use the same http_proxy env variable for http and https traffic, but the code is easy to follow if you need to change.