这本来应该很简单,但是我没有得到数据,而没有取回数据。
我在代理服务器后面,并且我不擅长从代理服务器后面发出请求。我发现只有一个教程在不使用外部模块的情况下使用代理服务器时解释了http请求,并且没有正确解释。
const http = require('http');
let options = {
host: '10.7.0.1',
port:8080,
path:'www.google.co.uk'
}
http.get(options,(res) => {
let str = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
str += chunk;
})
res.on('end', (str) => {
console.log(str);
})
})
10.7.0.1是代理服务器的地址,而8080是端口。
该代码的输出未定义。
我什至不知道该方法是否正确,也无法弄清楚。我阅读了节点http docs,并据我所知编写了options对象,如果我错了,请更正我。
通常,在代理服务器后面时如何发出请求。
该request
包允许以http
一种非常优雅的方式发出请求,并且为了指定代理,只需要proxy
在options对象所接受的options对象中作为参数设置key的值即可。
const request = require('request');
request({url: 'http://example.com', proxy: 'https://host:port'}, (error, response, body) => {
//Do whatever you want
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句