Moment For Technology

proxy in nodejs code

Posted on Oct. 3, 2022, 6:05 a.m. by 李雅琪
Category: The back-end Tag: The back-end

I train myslef with NodeJS and tried a simple GET call. Here is my code:

Var HTTP = the require (" HTTP ");

Var options = {host: 'www.boardgamegeek.com', path: '/ xmlAPI /boardgame/1? Stats =1', method: 'GET'}

Var request = http.request(options, function (response) {var STR = "" response.on(' data ', function (data) { str += data; }); Response. on(' end ', function () {console.log(STR); }); });

Request. On (' error ', function (e) {console.log('Problem with request: '+ e.message); });

request.end();

When behind a proxy you need to make the following modifications (as explained in this answer):

put the proxy host in the host parameter

put the proxy port in the port parameter

put the full destination URL in the path parameter :

Which gives:

Var options = {host: ' PROXY_HOST ', port: ' PROXY_PORT ,' path: 'www.boardgamegeek.com/xmlapi/boar... ', method: GET, headers: {Host: 'www.boardgamegeek.com'}}

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.