I need to use a VPN in a few parts of my NodeJS server but only when fetching from one APIs. Everything else in my server should work as normal. I am using WireGuard, I could use OpenVPN but I prefer WireGuard as I get much slower speeds with OpenVPN. I am not sure if this is possible and how I do it.
I have done some research and ended up writing this below in NodeJS and it did not work. I am not that knowledgeable about networking, so I do not understand everything. What I did notice is that when I run the command "Get-NetIPInterface" in PowerShell with WireGuard connected with my VPN normally, I can see it there. But when I disconnect from WireGuard it disappears, and I am not sure if that has anything to do with it, but that's all the information I can think of that could be of help since I am a bit clueless.
import http from "http";
import axios from "axios";
const interfaceWg = "---";
const agent = new http.Agent({
localAddress: interfaceWg,
});
const axiosWg = axios.create({
httpAgent: agent,
});
const url = "---";
// Make an HTTP request using the axios instance with the WireGuard agent
axiosWg
.get(url)
.then((response) => {
console.log("Response:", response.data);
})
.catch((error) => {
console.error("Error:", error);
});
It ends up just doing a normal get request as far as I can tell, when I do a get request on websites that show me my IP, I get my IP and not the one from the VPN like I want.