Skip to content

Instantly share code, notes, and snippets.

@nitzanav
Forked from mizzy/node-simple-proxy.js
Last active June 18, 2019 20:23
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 nitzanav/84033ffa8cbaf28a1139c93d5161e68f to your computer and use it in GitHub Desktop.
Save nitzanav/84033ffa8cbaf28a1139c93d5161e68f to your computer and use it in GitHub Desktop.
Simple proxy made by node.js - Async one that returns response immediately to the client. The response of the host is ignored.
var http = require('http');
var url = require('url');
var proxy = http.createServer(function(req, res) {
var request = url.parse(req.url);
options = {
host: request.hostname,
port: request.port || 80,
path: request.path,
method: req.method,
headers: req.headers,
};
var backend_req = http.request(options, function(backend_res) {
res.writeHead(backend_res.statusCode, backend_res.headers);
});
req.on('data', function(chunk) {
backend_req.write(chunk);
});
req.on('end', function() {
backend_req.end();
res.end();
});
});
proxy.listen(8000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment