Send HTTP request from different IPs in Node.js

16,865

In the node http module there is a localAddress option for binding to specific network interface.

var http = require('http');

var options = {
  hostname: 'www.example.com',
  localAddress: '202.1.1.1'
};

var req = http.request(options, function(res) {
  res.on('data', function (chunk) {
    console.log(chunk.toString());
  });
});

Check out Mikeal's Request on Github.

Tor uses SOCKS5 and these two modules can help: socks5-http-client and socks5-https-client

require('socks5-http-client').request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

Another option is to use a free web proxy, such as the Hide My Ass web proxy. They also provide a list of ip:port proxies which you can use. Both http, https and even SOCKS4/5. Either use the above modules or simply configure your web browser to use one of them.

You could even setup your own private http proxy node app and deploy on Heroku. I found a ton of easy to follow examples on Google.

Share:
16,865

Related videos on Youtube

user3050837
Author by

user3050837

Updated on September 15, 2022

Comments

  • user3050837
    user3050837 almost 2 years

    Is there a way to send an HTTP request with a different IP from what I have in Node.js?

    I want to send a request from an IP that I choose before, and not from the IP of the server or from my computer’s IP.

    I know that Tor Project does this kind of manipulation, but I didn't find any library that Tor uses to do this stuff.

    Is there any API or Node.js module that Tor uses to handle this kind of private browsing in Node.js?

  • Admin
    Admin over 10 years
    Note that this only allows you to send requests from IP addresses that are configured on your machine. You can't just use any old address!
  • Mike Causer
    Mike Causer over 10 years
    Yes yes, this example is for when your server has multiple IPs and you want to pick and choose.
  • Mike Causer
    Mike Causer over 10 years
    I've added a few more examples
  • Mike Causer
    Mike Causer over 10 years
    Has this been helpful?
  • user1665355
    user1665355 about 9 years
    @MikeCauser do you have any example about how to use HideMyAss with request?:)