Skip to content

Instantly share code, notes, and snippets.

@alessioalex
Created November 22, 2014 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alessioalex/1f22f96b70d4c780840c to your computer and use it in GitHub Desktop.
Save alessioalex/1f22f96b70d4c780840c to your computer and use it in GitHub Desktop.
http-debugging-proxy.js
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