'How can I send request via proxy server in nodejs?

In java, I can send request via proxy server as

HttpClient clinet = HttpClient.newBuilder()
        .proxy(ProxySelector.of(new InetSocketAddress("proxy.server", portNum)))
        .build();
HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("https://server.I.want.to.send.request"))
        .POST(HttpRequest.BodyPublishers.noBody())
        .build();

HttpResponse<String> response =  clinet.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());   

I have tested with code above and checked it works just fine.

But if I want to do the same thing in nodejs (react) it seems like it doesn't work.

const agent = httpsProxyAgent('proxy.server:portNum');

axios.request({
    method: 'POST',
    url: "https://server.I.want.to.send.request",
    data: {body},
    httpsAgent: agent
});

As far as I understand, code above is the best I can try with JavaScript in react.
I have tried it, but it fails with timeout error (which means request doesn't go through proxy server I guess)

Could anyone can give me an advise to solve this problem?



Solution 1:[1]

i think you can refer to this issue in github:

https://github.com/axios/axios/issues/925#issuecomment-513028175

If this still cannot fix your issue. You might change to use another library, node-fetch. The link is here:

https://www.scrapingbee.com/blog/proxy-node-fetch/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gary