'JS: send http request to a specific port
I have a MERN website that uses port 80 for the front-end and port 4000 for the back-end API. In MERN tutorials, they put axios.post('localhost:4000/login'...) to communicate with the backend API. But this won't work in production if I have a domain like example.com. Is there a way to use relative URLs to specify a port, like axios.post(':4000/login')?
Solution 1:[1]
You can create an axios instance like so:
export const myInstance = axios.create({
baseURL: backendUrl,
})
backendUrl should be an environment variable to your backend: https://production.com or http://localhost:4000
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 | Phung Nguyen |
