Created
November 22, 2014 14:47
-
-
Save alessioalex/1f22f96b70d4c780840c to your computer and use it in GitHub Desktop.
http-debugging-proxy.js
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 fs = require('fs'); | |
var http = require('http'); | |
var httpProxy = require('http-proxy'); | |
// | |
// Create a proxy server with custom application logic | |
// | |
var proxy = httpProxy.createProxyServer({}); | |
// | |
// Create your custom server and just call `proxy.web()` to proxy | |
// a web request to the target passed in the options | |
// also you can use `proxy.ws()` to proxy a websockets request | |
// | |
var server = http.createServer(function(req, res) { | |
if (req.url === '/package/http-proxy') { | |
res.setHeader('Content-Type', 'text/html'); | |
return fs.createReadStream(__dirname + '/homepage.html').pipe(res); | |
} | |
if (req.url === '/local.css') { | |
res.setHeader('Content-Type', 'text/css'); | |
return fs.createReadStream(__dirname + '/local.css').pipe(res); | |
} | |
// You can define here your custom logic to handle the request | |
// and then proxy the request. | |
proxy.web(req, res, { target: 'https://www.npmjs.org' }); | |
}); | |
console.log("listening on port 5050") | |
server.listen(5050); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment