Tag Archives: proxy

How to create a simple http proxy in node.js?

I'm trying to create a proxy server to pass HTTP GET requests from a client to a third party website (say google). My proxy just needs to mirror incoming requests to their corresponding path on the target site, so if my client's requested url is:

127.0.0.1/images/srpr/logo11w.png

the following resource should be served:

http://www.google.com/images/srpr/logo11w.png

here is what I came up with:

http.createServer(onRequest).listen(80);

function onRequest (client_req, client_res) {
    client_req.addListener("end", function() {
        var options = {
            hostname: 'www.google.com',
            port: 80,
            path: client_req.url,
            method: 'GET'
        };
        var req=http.request(options, function(res) {
            var body;
            res.on('data', function (chunk) {
                body += chunk;
            });
            res.on('end', function () {
                 client_res.writeHead(res.statusCode, res.headers);
                 client_res.end(body);
            });
        });
        req.end();
    });
}

it works well with html pages, but for other types of files, it just returns a blank page or some error message from target site (which varies in different sites).

How to create a simple http proxy in node.js?

Salam (means hello) :)

I'm trying to create a proxy server to pass HTTP GET requests from a client to a third party website (say google). My proxy just needs to mirror incoming requests to their corresponding path on the target site, so if my client's requested url is:

127.0.0.1/images/srpr/logo11w.png

the following resource should be served:

http://www.google.com/images/srpr/logo11w.png

here is what I came up with:

http.createServer(onRequest).listen(80);

function onRequest (client_req, client_res) {
    client_req.addListener("end", function() {
        var options = {
            hostname: 'www.google.com',
            port: 80,
            path: client_req.url,
            method: 'GET'
        };
        var req=http.request(options, function(res) {
            var body;
            res.on('data', function (chunk) {
                body += chunk;
            });
            res.on('end', function () {
                 client_res.writeHead(200, { 'Content-Type': mime_of(client_req.url)});
                 client_res.end(body);
            });
        });
        req.end();
    });
}

it works well with html pages, but for other types of files, it just returns a blank page or some error message from target site (which varies in different sites).

Proper settings for socket.io flash (and other) fallbacks for clients using a proxy

Salam (means hello) :)

I have the following basic settings in my node.js application. But it doesn't work for clients who are connected using a proxy or are behind a firewall. I want to know what should I add to get socket.io fallback to work as expected:

Server side:

var io = require('socket.io').listen(3000, {
    log: 3,
    flashPolicyServer: true,
    transports: ['htmlfile', 'xhr-polling', 'jsonp-polling', 'flashsocket']
});

io.sockets.on('connection', function(){
    // my event handlers
});

Client side:

WEB_SOCKET_SWF_LOCATION = "oath/to/my/copy/of/WebSocketMain.swf";
var socket = io.connect('http://localhost:3000');
// my event handlers

Proper settings for socket.io flash (and other) fallbacks for clients using a proxy

Salam (means hello) :)

I have the following basic settings in my node.js application. But it doesn't work for clients who are connected using a proxy or are behind a firewall. I want to know what should I add to get socket.io fallback to work as expected:

Server side:

var io = require('socket.io').listen(3000, {
    log: 3,
    flashPolicyServer: true,
    transports: ['htmlfile', 'xhr-polling', 'jsonp-polling', 'flashsocket']
});

io.sockets.on('connection', function(){
    // my event handlers
});

Client side:

WEB_SOCKET_SWF_LOCATION = "oath/to/my/copy/of/WebSocketMain.swf";
var socket = io.connect('http://localhost:3000');
// my event handlers