Created
September 8, 2015 23:11
-
-
Save a2ndrade/3d6d11a9de1ca8ced7a3 to your computer and use it in GitHub Desktop.
proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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