Skip to content

Instantly share code, notes, and snippets.

@j1cs
Created February 24, 2019 23:45
Show Gist options
  • Save j1cs/897886ec007ef9fb5d4452f3332d4f97 to your computer and use it in GitHub Desktop.
Save j1cs/897886ec007ef9fb5d4452f3332d4f97 to your computer and use it in GitHub Desktop.
'use strict';
const HttpsProxyAgent = require('https-proxy-agent');
/*
* API proxy configuration.
* This allows you to proxy HTTP request like `http.get('/api/stuff')` to another server/port.
* This is especially useful during app development to avoid CORS issues while running a local server.
* For more details and options, see https://github.com/angular/angular-cli#proxy-to-backend
*/
const proxyConfig = [
{
context: '/api',
pathRewrite: {'^/api': '/api/v1'},
target: 'http://localhost:80',
changeOrigin: true,
secure: false
}
];
/*
* Configures a corporate proxy agent for the API proxy if needed.
*/
function setupForCorporateProxy(proxyConfig) {
if (!Array.isArray(proxyConfig)) {
proxyConfig = [proxyConfig];
}
const proxyServer = process.env.http_proxy || process.env.HTTP_PROXY;
let agent = null;
if (proxyServer) {
console.log(`Using corporate proxy server: ${proxyServer}`);
agent = new HttpsProxyAgent(proxyServer);
proxyConfig.forEach(entry => { entry.agent = agent; });
}
return proxyConfig;
}
module.exports = setupForCorporateProxy(proxyConfig);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment