429 Error Code from axios api GET request - node.js

I'm having an issue with a 429 code error calling for too many requests that I am completely unsure how to solve. I'm getting the error when trying to make an axios api call to my Express server from my React frontend app.
Here is the backend route:
app.get('/api/fetchproducts', async (req, res) => {
const products = await Product.find({});
res.send(products);
});
And here is the axios call from the frontend:
import axios from 'axios';
import { FETCH_PRODUCTS } from "./types";
export const fetchProducts = () => async dispatch => {
const res = await axios.get('/api/fetchproducts');
dispatch({ type: FETCH_PRODUCTS, payload: res.data });
};
The error in the console is:
Uncaught (in promise) Error: Request failed with status code 429
Can anyone help me with this issue?
Please let me know if further information is required

this kind of an error normally occurs when your API is having rate-limiting enabled.

Related

Axios Problems (uncaught in promise) using zustand

Hi there i have problems with making the api call to my server. Using Postman i receive correctly my data from the URL but when i come into react and i use Axios to get those data i have the Axios error Uncaught in promise status code: 300 Here my code.
import {create} from 'zustand'
import axios from 'axios'
const URL1 = "http://localhost:5000/livespa"
export const useStore = create((set, get) => ({
liveSpa:[],
loadLivespa: async () => {
const response = await axios.get(URL1)
set({ liveSpa: await response.data})
},
}))
And my frontend page is like
const LiveSpa = () => {
const data = useStore((state) => state.liveSpa)
const loadBets = useStore((state)=> state.loadLivespa)
useEffect(() => {
loadBets()
}, [])
return (...)
}
I tried using Fetch but same problems, debugging my node server but it works fine using Postman

Why am I am Getting a Typeerror: res.send is not a function with Axios. Res.send works outside the res.send but I need the data from the API

enter image description here
I need to get data from this api endpoint, and send it to my client (React). Everything works fine between the frontend and backend, but I cant seem to figure out how to get the data within /dailyscores endpoint and send it using axios. Any help on why res.send is not a function inside .then, and a way to get it work?
The way you are using res as argument name for both express and axios callback is the issue here.
app.get('...', (req, res) => {
axios.get('...').then((res) => {
res.send(res.data); // here the res of axios.then is used
})
});
Instead use different names
app.get('...', (req, res) => {
axios.get('...').then((response) => {
res.send(response.data);
})
});
checkout variable scopes for more info
To get data from API please try as below:
const [apiResp, setApiResp] = useState([]);
useEffect(() => {
axios.get(`<api>`)
.then(res => {
const response = res.data;
setApiResp([response]);
})
});

How to call an external API from my API in Koa.js

I have an issue where I want to call an external API to verify a certain token coming from my frontend.
i.e
router.post('/api/verifyToken', async (ctx, next) => {
router.post('https://external-site.com/verify').then((response) => {
if (response.success) { console.log('Do some stuff') };
})
})
How would I go about doing this with koa?
You misunderstood with the router itself.
In your router, you define a route where your clients can send HTTP requests and according to your business logic, you return the answers to them.
You can simply imagine router.post('/api/verifyToken' as an event listener. When a request comes in you run whatever is inside of it. It is not an HTTP client though.
If you want to send an external request you have to use an HTTP client for it.
There are a bunch of options:
Built-in http/https package
Axios
Node-fetch
GOT
Request
And many others
One simple example how to do with Axios would be
import axios from 'axios';
router.post('/api/verifyToken', async (ctx, next) => {
try{
const response = await axios.post('https://external-site.com/verify');
// Do your stuff here
console.log(response.data);
ctx.body = response.data;
}catch(e){
ctx.status = 422;
ctx.body = e.message;
}
})

How to scrape a website from opensea using node js

I'm failing at trying to scrape a collection of polygon NFTs from Opensea. Could someone please provide an example that prints the html returned in console? I tried the code below:
const https = require("https");
https.get("https://opensea.io/collection/orathai", response => {
console.log(response)
});
const axios = require('axios').default;
axios.get('https://api.opensea.io/api/v1/collection/orathai/?format=json')
.then(resp => {
console.log(resp.data);
})
.catch(err => {
// Handle Error Here
console.error(err);
});
don't forget : npm i axios
Refrence :
opensea api docs
Axios

Using Node and Express, How to Call remote API from inside server.get(..)

Because of CORS problems, I want to call an external REST API from inside my node express server. That is, I have code like this that obviously does not work because it does not return.
How can I make this work and return the results of my external call?
const server = express();
server.put('/callme',(req,res) => {
axios
('http://weather.com/restapi', 'put', { zip: 10530 })
.then((resp: any) => {
console.log(' success' + resp.data);
})
.catch(function(error: any) {
console.log(error.message);
});
}
Axios returns a Promise which is resolved in the .then(). In order to get the response data back to the client you need to return it with res.send().
const server = express();
server.get('/callme', (req, res) => {
axios
.get('http://weather.com/restapi?zip=10530')
.then((resp: any) => {
res.send(resp.data);
})
.catch(function(error: any) {
console.log(error.message);
});
}
It would be a good idea to cache the weather API response for a period of time and serve the cached response for subsequent requests.

Resources