i edited nothing at server, just add proxyServer to fix cors
About that path, its authorâs code⊠i have just update new link url of 1inchâs api /swap/v6.0/
This is my code at server:
const express = require(âexpressâ);
const Moralis = require(âmoralisâ).default;
const { createProxyMiddleware } = require(âhttp-proxy-middlewareâ);
const cors = require(âcorsâ);
require(âdotenvâ).config();
const axios = require(âaxiosâ);
const app = express();
const port = 3001;
// Middleware Äá» cho phĂ©p táș„t cáșŁ cĂĄc nguá»n truy cáșp (CORS)
app.use(cors());
app.use(express.json());
// Proxy middleware Äá» chuyá»n tiáșżp yĂȘu cáș§u Äáșżn 1inch API
app.use(
â/swapâ,
createProxyMiddleware({
target: âhttps://api.1inch.devâ,
changeOrigin: true,
pathRewrite: {
â^/swapâ: ââ, // Rewrites the URL for the target
},
onProxyReq: (proxyReq) => {
proxyReq.setHeader(
âAuthorizationâ,
Bearer ${process.env.REACT_APP_1INCH_KEY}
);
},
})
);
app.get("/tokenPrice", async (req, res) => {
const { query } = req;
try {
const responseOne = await Moralis.EvmApi.token.getTokenPrice({
address: query.addressOne,
});
const responseTwo = await Moralis.EvmApi.token.getTokenPrice({
address: query.addressTwo,
});
const usdPrices = {
tokenOne: responseOne.raw.usdPrice,
tokenTwo: responseTwo.raw.usdPrice,
ratio: responseOne.raw.usdPrice / responseTwo.raw.usdPrice,
};
return res.status(200).json(usdPrices);
} catch (error) {
console.error(error);
return res.status(500).json({ message: âError fetching token pricesâ });
}
});
// Khá»i Äá»ng Moralis vĂ backend server
Moralis.start({
apiKey: process.env.MORALIS_KEY,
}).then(() => {
app.listen(port, () => {
console.log(Listening for API Calls on port ${port}
);
});
});