This is my setupProxy.js:
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = (app) => {
app.use(
'http://localhost:8000',
createProxyMiddleware({
target: 'https://my-website.com', // Should request this url
changeOrigin: true,
})
);
};
And this is how I am calling the API:
makeAPICall = (method, headers, data) => {
let url = new Url('http://localhost:8000/api/get-data');
let requestOptions = {
method,
headers,
'body': data
}
return fetch(url, requestOptions);
}
But the API calls are going to the localhost and not the one that I mentioned in the setupProxy.
I know this is not right way to call apis when using proxy middleware, and we just need to provide a simple string in app.use
like '/api1'
. But I am not allowed to change the urls, just want to reroute them to new api.
What am I doing wrong here?
Edit: This is how I call makeAPICall function:
headers = {
'Content-Type': 'application/json',
'Authorization': `Token ${user.auth_token}`
}
makeAPICall('POST', headers, payload).then(data => handleData(data));