Last active
May 23, 2017 09:26
-
-
Save bichotll/ca13563e6a6f5c97573e1294f0a6ea6d to your computer and use it in GitHub Desktop.
Proxy modifying and setting up permissive headers
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
{ | |
"name": "web-proxy", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"dependencies": { | |
"http-proxy": "^1.16.2" | |
}, | |
"devDependencies": {}, | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC" | |
} |
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
var http = require('http'), | |
httpProxy = require('http-proxy'); | |
// | |
// Create a proxy server with custom application logic | |
// | |
var proxy = httpProxy.createProxyServer({}); | |
// To modify the proxy connection before data is sent, you can listen | |
// for the 'proxyReq' event. When the event is fired, you will receive | |
// the following arguments: | |
// (http.ClientRequest proxyReq, http.IncomingMessage req, | |
// http.ServerResponse res, Object options). This mechanism is useful when | |
// you need to modify the proxy request before the proxy connection | |
// is made to the target. | |
// | |
proxy.on('proxyReq', function(proxyReq, req, res, options) { | |
}); | |
var server = http.createServer(function(req, res) { | |
//setting headers | |
res.setHeader('X-Special-Proxy-Header', 'foobar'); | |
res.setHeader('Access-Control-Allow-Origin', '*'); | |
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); | |
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); | |
res.setHeader('Access-Control-Allow-Credentials', true); | |
// You can define here your custom logic to handle the request | |
// and then proxy the request. | |
proxy.web(req, res, { | |
target: 'https://ur-url.com', | |
secure: false | |
}); | |
}); | |
console.log('proxy started. port 8000.'); | |
server.listen(8000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment