Create Proxy MIddleware correctly

Viewed 34

This is the url I need to access:

https://testnet-fx-rest.io/cosmos/staking/v1beta1/delegations"

I'm trying to use createProxyMiddleware to bypass CORS and these are my codes:

setupProxy.js

const { createProxyMiddleware } = require("http-proxy-middleware");

module.exports = function (app) {
    app.use(
        "/TestnetApi",
        createProxyMiddleware({
            target: "https://testnet-fx-rest.io/cosmos",
            changOrigin: true,
        })
    )
};

method that calls the GET request:

    getDelegatorDelegations: async() => {
        try {
            let delegationArr = [];
            const res = await axios.get(`/TestnetApi/staking/v1beta1/delegations`);
            res.data.delegation_responses.map(del => {
                delegationArr.push(del);
            })
            return delegationArr
        }
        catch (err) {
            console.log(err.response.data);
        }
    },

However when my method is executed, I get the error:

Error occurred while trying to proxy: localhost:3000/TestnetApi/staking/v1beta1/delegations

I'm not sure why is my GET request that way, what am I doing wrong? Do i have to add proxy in my package.json?

0 Answers
Related