V
V
vetsmen2017-09-18 19:19:35
JavaScript
vetsmen, 2017-09-18 19:19:35

How to use proxy in node.js?

There is a request library: https://github.com/request/request#proxies
How can I use it to send a get request to a specific address through a specific proxy server? I did not find a code example there, maybe someone has already worked with this task

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2017-09-18
@vetsmen

It works quite well:

const request = require('request');
request.get({
  url: 'https://toster.ru/q/461924',
  proxy: 'http://195.209.176.2:8080'
}, (err, res) => {
  if (err) {
    console.log('ERROR', err);
  } else {
    console.log('OK', res);
  }
});

I
IL27, 2018-04-27
@IL27

I settled on the option:
1. npm install tunnel
2. connect this module:
global.tunnel = require('tunnel');
or
var tunnel = require('tunnel');
3. in the request options there is an "agent" parameter, it must be defined as follows:
...for the case of HTTP...
agent: tunnel.httpOverHttp( { proxy: { host: "proxy addr", port: 3128, headers: "add's headers for request" } } )
...for the HTTPS case... agent: tunnel.httpsOverHttp
( { proxy: { host: "proxy addr", port: 3128, headers: "add's headers for request" } } )
I tried and checked in my project for the Telegram bot.
I had to, because of the latest actions of the RKN

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question