简体   繁体   English

如何在npm的请求包中使用代理

[英]How to use proxy in npm's request package

In the code bellow I'm trying to use request with a proxy. 在下面的代码中,我试图将请求与代理一起使用。 To check that I use https://v4.ident.me/ to verify. 要检查我是否使用https://v4.ident.me/进行验证。

The documentation does not provide examples so I tried the code bellow but it is printing my actual IP, not the proxie's: 文档没有提供示例,因此我尝试了下面的代码,但是它在打印我的实际IP,而不是代理的IP:

const REQUEST = require('request'); // https://www.npmjs.com/package/request
var r_options = {
    host: '<proxy ip address>', // http://www.freeproxylists.net/
    port: 8080,
    timeout: 30000, // 30s
    headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0'
    },
    url: 'https://v4.ident.me/'

};

REQUEST(r_options, function (error, response, body) {
    console.log(body);
});

So, how to use proxy in npm's request package? 那么,如何在npm的请求包中使用代理?

The documentation does not provide any examples on proxy. 该文档未提供有关代理的任何示例。

Here is how to do it: 这是操作方法:

const REQUEST = require('request'); // https://www.npmjs.com/package/request

var options = {
 url: 'http://www.somewhere.com',
 proxy: 'http://<myproxy>:<portNumber>'
}

REQUEST(options, function (error, response, body) {

   // your code here

});

Note that to access https websites you need a proxy that supports https . 请注意,要访问https网站,您需要支持https的代理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM