帮助文档>代码示例 > Node语言代码示例

Node语言代码示例

发布时间:2022-07-03 00:49

nodejs http

  1. const http = require("http");
  2. const url = require("url");
  3.  
  4. const targetURL = url.parse("http://myip.ipip.net");
  5. const proxyIp = "219.151.125.106";
  6. const proxyPort = "31615";
  7. const authKey = "895314XY";
  8. const password = "24D6YB309ZCB";
  9.  
  10. const base64 = new Buffer.from(authKey + ":" + password).toString("base64");
  11.  
  12. const options = {
  13. host: proxyIp,
  14. port: proxyPort,
  15. path: targetURL,
  16. method: "GET",
  17. headers: {
  18. "Host": urlParsed.hostname,
  19. "Proxy-Authorization" : "Basic " + base64
  20. }
  21. };
  22.  
  23. http.request(options, function (resp) {
  24. console.log("response status code: " + resp.statusCode);
  25. resp.pipe(process.stdout);
  26. }).on("error", function (err) {
  27. console.log("request failed: " + err);
  28. }).end();

nodejs request

  1. const request = require("request");
  2.  
  3. const targetUrl = "http://myip.ipip.net";
  4. const proxyIp = "219.151.125.106";
  5. const proxyPort = 31615;
  6. const authKey = "895314XY";
  7. const password = "24D6YB309ZCB";
  8.  
  9. const proxyUrl = "http://" + authKey + ":" + password + "@" + proxyIp + ":" + proxyPort;
  10. const req = request.defaults({'proxy': proxyUrl});
  11.  
  12. const options = {
  13. url : targetUrl,
  14. headers: {}
  15. };
  16.  
  17. req.get(options, function (err, resp, body) {
  18. if (err) {
  19. return console.log(err);
  20. }
  21. console.log("response status code: " + resp.statusCode);
  22. console.log("response body: " + body);
  23. }).on("error", function (err) {
  24. console.log("request failed: " + err);
  25. });

nodejs superagent

  1. const request = require("superagent");
  2.  
  3. require("superagent-proxy")(request);
  4.  
  5. const targetUrl = "http://myip.ipip.net";
  6. const proxyIp = "219.151.125.106";
  7. const proxyPort = 31615;
  8. const authKey = "895314XY";
  9. const password = "24D6YB309ZCB";
  10.  
  11. const proxyUrl = "http://" + authKey + ":" + password + "@" + proxyIp + ":" + proxyPort;
  12.  
  13. request.get(targetUrl).proxy(proxyUrl).end(function onResponse(err, resp) {
  14. if (err) {
  15. return console.log(err);
  16. }
  17.  
  18. console.log("response status code: " + resp.statusCode);
  19. console.log("response body: " + resp.text);
  20. });

nodejs axios

  1. const axios = require('axios');
  2.  
  3. const targetUrl = "http://myip.ipip.net";
  4. const proxyIp = "219.151.125.106";
  5. const proxyPort = 31615;
  6. const authKey = "895314XY";
  7. const password = "24D6YB309ZCB";
  8.  
  9. var proxy = {
  10. host: proxyIp,
  11. port: proxyPort,
  12. auth: {
  13. username: authKey,
  14. password: password
  15. }
  16. };
  17.  
  18. axios.get(targetUrl, {proxy:proxy}).then(function (response) {
  19. console.log("response body: " + response.data);
  20. }).catch(function (error) {
  21. console.log("request failed: " + error);
  22. }).finally(function () {
  23. console.log("request finished.")
  24. });
本文导读