How to create a simple http proxy in node.js?
I don't think it's a good idea to process response received from the 3rd party server. This will only increase your proxy server's memory footprint. Further, it's the reason why your code is not working.
Instead try passing the response through to the client. Consider following snippet:
var http = require('http');
http.createServer(onRequest).listen(3000);
function onRequest(client_req, client_res) {
console.log('serve: ' + client_req.url);
var options = {
hostname: 'www.google.com',
port: 80,
path: client_req.url,
method: client_req.method,
headers: client_req.headers
};
var proxy = http.request(options, function (res) {
client_res.writeHead(res.statusCode, res.headers)
res.pipe(client_res, {
end: true
});
});
client_req.pipe(proxy, {
end: true
});
}
Creating A HTTP proxy server with https support and use another proxy server to serve the response using nodejs
I have found a really super simple solution to the problem. We can just forward all packets as it is to the proxy server. and still can handle the server logic with ease.
var net = require('net');
const server = net.createServer()
server.on('connection', function(socket){
var laddr = socket.remoteAddress;
console.log(laddr)
var to = net.createConnection({
host: "<Proxy IP>",
port: <Proxy Port>
});
socket.pipe(to);
to.pipe(socket);
});
server.listen(3000, "0.0.0.0");
How can I use an http proxy with node.js http.Client?
Tim Macfarlane's answer was close with regards to using a HTTP proxy.
Using a HTTP proxy (for non secure requests) is very simple. You connect to the proxy and make the request normally except that the path part includes the full url and the host header is set to the host you want to connect to.
Tim was very close with his answer but he missed setting the host header properly.
var http = require("http");
var options = {
host: "proxy",
port: 8080,
path: "http://www.google.com",
headers: {
Host: "www.google.com"
}
};
http.get(options, function(res) {
console.log(res);
res.pipe(process.stdout);
});
For the record his answer does work with http://nodejs.org/ but that's because their server doesn't care the host header is incorrect.
Forward http proxy using node http proxy
To use a dynamic target, you should create a regular HTTP server that uses a proxy instance for which you can set the target dynamically (based on the incoming request).
A bare bones forwarding proxy:
const http = require('http');
const httpProxy = require('http-proxy');
const proxy = httpProxy.createProxyServer({});
http.createServer(function(req, res) {
proxy.web(req, res, { target: req.url });
}).listen(9000, () => {
console.log("Waiting for requests...");
});
use http-proxy inside a express JS server
I think it is better to use this package if you use express: https://www.npmjs.com/package/express-http-proxy
You can use it like this:
const { createProxyMiddleware, fixRequestBody, responseInterceptor } = require( 'http-proxy-middleware' );
const proxyMiddleware = createProxyMiddleware({
target: 'foo',
onProxyReq: fixRequestBody,
logLevel: 'debug',
changeOrigin: true,
secure: false,
xfwd: true,
ws: true,
hostRewrite: true,
cookieDomainRewrite: true,
headers: {
"Connection": "keep-alive",
"Content-Type": "text/xml;charset=UTF-8",
"Accept": "*/*"
},
});
app.use( '/youRoute/**', proxyMiddleware );
And then when you are loged in you redirect to you you proxified route :
res.redirect('/youRoute/');
Related Topics
Dot and Square Bracket Notation
Checking Whether Something Is Iterable
JavaScript Ie Detection, Why Not Use Simple Conditional Comments
Remembering State Before App Goes in Foreground
How to Open a Popup with JavaScript and Then Detect When the User Closes It
Add Cors Header to an Http Request Using Ajax
How to Detect Chrome and Safari Browser (Webkit)
What Are Alternatives to Extjs
When Should I Use a Semicolon After Curly Braces
Jslint Is Suddenly Reporting: Use the Function Form of "Use Strict"
How to Get All Checked Checkboxes
Angularjs $Resource Restful Example
Es6 Modules Implementation, How to Load a JSON File
.Append(), Prepend(), .After() and .Before()