Skip to content

Instantly share code, notes, and snippets.

@a2ndrade
Created September 8, 2015 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save a2ndrade/3d6d11a9de1ca8ced7a3 to your computer and use it in GitHub Desktop.
Save a2ndrade/3d6d11a9de1ca8ced7a3 to your computer and use it in GitHub Desktop.
proxy
const httpProxy = require('http-proxy');
module.exports = function(target) {
const proxy = httpProxy.createProxyServer({
target: target,
secure: false,
changeOrigin: true,
auth: 'user:password'
});
proxy.on('proxyReq', function(proxyReq, req, res, options) {
console.log('----------------------------------');
console.log(req.method + ': ' + target + req.url);
proxyReq.setHeader('Origin', target);
});
proxy.on('proxyRes', function (proxyRes, req, res) {
console.log(proxyRes.statusCode + ' ' + proxyRes.statusMessage + ' ' + proxyRes.headers['content-type']);
});
proxy.on('error', function(err, req, res) {
console.log(err);
res.writeHead(500, {'Content-type': 'text/plain'});
res.end('Could not connect to [' + req.url + ']. Error: ' + err);
});
return proxy;
};
/*
const express = require('express');
const proxy = require('./proxy.js')('http://your.proxy.server');
const app = express();
app.all('/some/url/*', function(req,res) {
proxy.web(req, res);
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment