最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How Do I Resolve this 'Too Many Requests' Error in Node.js? - Stack Overflow

programmeradmin2浏览0评论

I'm trying to build a JSON file by making successive HTTP requests with Axios:

  1. Get an array of objects (projects)
  2. Create an array property in each project named attachments
  3. Get each project's tasks
  4. Get each task's attachments
  5. Push each project's task's attachments in to the project's attachments array
  6. Create a JSON file out of the modified projects array

Code:

let getProjects = function() {
  try {
    return axios.get('.0/projects/')
  } catch (error) {
    console.error(error)
  }
}

let getTasks = function(project) {
  try {
    return axios.get('.0/projects/'+project+'/tasks')
  } catch (error) {
    console.error(error)
  }
}

let getAttachments = function(task) {
  try {
    return axios.get('.0/tasks/'+task+'/attachments')
  } catch (error) {
    console.error(error)
  }
}

async function getAsanaData() {
  let projects = await getProjects()
  return Promise.all(projects.data.data.map(async (project) => {
      project.attachments = []
      let tasks = await getTasks(project.gid)
      return Promise.all(tasks.data.data.map(async (task) => {
        let attachments = await getAttachments(task.gid)
        project.attachments = !!attachments ? project.attachments.concat(attachments.data.data) : project.attachments 
        return project
      }))
  }))
}

getAsanaData()
.then((projects) => {  
  var asanaData = safeJsonStringify(projects);
  fs.writeFile("thing.json", asanaData);
})
.catch(err=>console.log(err))

But I'm running into this error:

status: 429,
statusText: 'Too Many Requests

I haven't found anything helpful yet for figuring out how to resolve it. What can I do?

I'm trying to build a JSON file by making successive HTTP requests with Axios:

  1. Get an array of objects (projects)
  2. Create an array property in each project named attachments
  3. Get each project's tasks
  4. Get each task's attachments
  5. Push each project's task's attachments in to the project's attachments array
  6. Create a JSON file out of the modified projects array

Code:

let getProjects = function() {
  try {
    return axios.get('https://app.asana./api/1.0/projects/')
  } catch (error) {
    console.error(error)
  }
}

let getTasks = function(project) {
  try {
    return axios.get('https://app.asana./api/1.0/projects/'+project+'/tasks')
  } catch (error) {
    console.error(error)
  }
}

let getAttachments = function(task) {
  try {
    return axios.get('https://app.asana./api/1.0/tasks/'+task+'/attachments')
  } catch (error) {
    console.error(error)
  }
}

async function getAsanaData() {
  let projects = await getProjects()
  return Promise.all(projects.data.data.map(async (project) => {
      project.attachments = []
      let tasks = await getTasks(project.gid)
      return Promise.all(tasks.data.data.map(async (task) => {
        let attachments = await getAttachments(task.gid)
        project.attachments = !!attachments ? project.attachments.concat(attachments.data.data) : project.attachments 
        return project
      }))
  }))
}

getAsanaData()
.then((projects) => {  
  var asanaData = safeJsonStringify(projects);
  fs.writeFile("thing.json", asanaData);
})
.catch(err=>console.log(err))

But I'm running into this error:

status: 429,
statusText: 'Too Many Requests

I haven't found anything helpful yet for figuring out how to resolve it. What can I do?

Share Improve this question edited Jun 10, 2023 at 11:03 O. Jones 109k17 gold badges129 silver badges181 bronze badges asked Nov 3, 2018 at 2:08 Gabriel RiveraGabriel Rivera 1751 gold badge1 silver badge6 bronze badges 5
  • Sounds like, as it says, you're making too many requests, perhaps - how many requests are you making in a row with your current code? – CertainPerformance Commented Nov 3, 2018 at 2:09
  • You need to throttle your requests - asana is likely limiting you. The use of underscore or lodash is remended. Both have a throttle method to slow down the velocity of your requests. – Randy Casburn Commented Nov 3, 2018 at 2:11
  • @CertainPerformance Apologies; I don't yet know how to get feedback like number of requests attempted. But having an idea of the data, it's probably in the several thousands. – Gabriel Rivera Commented Nov 3, 2018 at 2:15
  • READ: asana./developers/documentation/getting-started/rate-limits – Randy Casburn Commented Nov 3, 2018 at 2:15
  • How big is the projects.data.data array? – jfriend00 Commented Nov 3, 2018 at 2:35
Add a ment  | 

3 Answers 3

Reset to default 0

HTTP response status code 429 indicates sending too many requests than what server could handle. It has been documented at https://asana./developers/documentation/getting-started/errors too. The maximum allowed is 150 per minute as documented at https://asana./developers/documentation/getting-started/rate-limits.

So, yes, as @Randy Casburn mented, you will have to throttle your requests.

You're getting throttled by Asana for sending too many requests and reaching the maximum rate.

When it happens, you need to check for the Retry-After response header and wait for the specified amount of time before sending another request.

https://asana./developers/documentation/getting-started/rate-limits

You can also learn more in the RFC 6585 about HTTP 429

if you are facing 429 error (Too Many Requests) : a mon error message that typically appears on your browser when you try to access a website or data , but your request has been denied or our server reached the limit basically we have 2 Algorithm to resolving this problem

1:- Leaky Bucket Algorithm ; 2:-Sliding Windows Algorithm

In node js we have npm install express express-rate-limiter using this library we will control our API request:

express-rate-limiter: is a middleware for rate limiting in Express.js applications. It allows us to control the rate at which requests are allowed to our Express routes.

const setRateLimit = require("express-rate-limit");

// Rate limit middleware
const rateLimitMiddleware = setRateLimit({
  windowMs: 60 * 1000,
  max: 5,
  message: "You have exceeded your 5 requests per minute limit.",
  headers: true,
});

module.exports = rateLimitMiddleware;  

windowMs: This is the window (time frame) size in milliseconds. max: Maximum number of requests which can be allowed in the given window size. message: This option is optional, we can customize the error message or use the default message provided by the middleware. headers: The headers option is essential, as it automatically adds crucial HTTP headers to responses

In server.js or root file :

const express = require("express");
const rateLimitMiddleware = require("./middlewares/ratelimit");
const app = express();

app.use(rateLimitMiddleware);
发布评论

评论列表(0)

  1. 暂无评论