How to set proxy when using axios to send requests?

Tarun Khare

I am using a package called 'concurrently' to run my client and server at the same time on localhost. Client runs on port 3000 while server runs on port 5000. I have set proxy in the package.json of server in the following manner:

"proxy": "https://localhost:5000"

But when I make a request from client in the following manner:

    const config = {
        headers: {
          'Content-Type': 'application/json'
        }
      };

    const res = await axios.post('/api/users', body, config);

It says: POST http://localhost:3000/api/users 404 (Not Found). I don't understand why but despite setting proxy, axios keeps making request to port 3000 instead of port 5000. What is the issue?

Tarun Khare

I got it working correctly. What I did was:

1) change axios.post('/api/users', body, config); to axios.post('http://localhost:5000/api/users', body, config);

2) Then in the 'users' express route on the server side, add CORS functionality by installing 'cors' npm package, and then adding the following lines:

const router = express.Router();
...
// add these lines
var cors = require('cors');
router.use(cors()); 
...
router.post('/', async (req, res) => {
...
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to send asynchronous requests by guzzle without using proxy

When I set up SSL/TLS in React and Spring using CloudFlare, no axios requests are made from React to Spring

How to send n axios post requests depending on number of elements in array

How to properly send GET and CONNECT requests from proxy to client?

send all .xml requests to proxy

How can I send an object using axios?

Using Axios how to send username in GET request

how to send images through axios using gridfs?

Proxy request error when trying to send post request from React to Node, proxy is working fine on get requests

Using promises in Axios requests

How to send XML POST requests using JMeter

how to send multiple requests using same client?

How to send multiple HTTP requests using Python

How to send bulk get requests using nodejs?

How can I override the set axios override defaults on specific requests?

How to send GET requests with JSON body using requests 2.18?

How to send multiple 'GET' requests using get function of requests library?

How to authorise send requests using GOOGLE_APPLICATION_CREDENTIALS environment variable when deploying to Render?

How can I set a single proxy for a requests session object?

how to use proxy for axios

Set up a proxy with python requests

How to use proxy in requests

Cannot send form data with axios when using interceptors

Getting error message when trying send request to server using axios

How to make multiple Axios requests using a dynamic array of links in Javascript?

Make Axios send cookies in its requests automatically

React / Axios send infinite number of requests

error when trying to send a video using requests library Python

How to proxy all requests to localhost using nginx and plesk?