简体   繁体   English

向服务器发送多个请求的最佳方式是什么?

[英]What's the best way to send multiple requests to a server?

first off, I am writing in node.js using modules discord.js, request, and request-promise.首先,我在 node.js 中使用模块 discord.js、request 和 request-promise 编写代码。 I am having trouble sending a ton (about 70) different requests to a server.我无法向服务器发送大量(大约 70 个)不同的请求。 I keep getting the same result from the different requests.我不断从不同的请求中得到相同的结果。 'https://api.mojang.com/user/profiles/' + entry.uuid + '/names' That is the request I am making, I have a forEach loop going through a list of urls like this. 'https://api.mojang.com/user/profiles/' + entry.uuid + '/names'这就是我提出的请求,我有一个 forEach 循环遍历这样的 url 列表。 Here is my code for the requests:这是我的请求代码:

function getNames() {
    count = 0;
    msg.channel.send("Started getting Names");
    urls.forEach(function() {
        request(urls[count], function(err, res, body) {
            if (!err && res.statusCode == 200) {
                const obj = JSON.parse(body);
                names[count] = body[0].name;
                msg.channel.send(urls[count] + ", Username " + count + ": " + obj[0].name);
            } else {
                msg.channel.send("Username Error: " + err);
            }
        });
        count++;
    });
    while (true) {
        if (names.length != urls.length) {
            msg.channel.send("Finished getting Names");
            console.log("UUIDs:" + urls.join(", "));
            console.log("Names:" + names.join(", "));
            break;
        }
    }
}

The msg.channel.send commands I am using for debugging my code (discord.js if you are curious).我用于调试代码的msg.channel.send命令(如果您好奇,可以使用 discord.js)。 Now, my problem is this: msg.channel.send(urls[count] + ", Username " + count + ": " + obj[0].name);现在,我的问题是: msg.channel.send(urls[count] + ", Username " + count + ": " + obj[0].name); always gives me a new url but the same username as the first request ( obj[0].name is always the same).总是给我一个新的 url 但与第一个请求相同的用户名( obj[0].name始终相同)。 So, say I have 3 different urls the first URLs username is Evan, second is Jack, third is John.所以,假设我有 3 个不同的 URL,第一个 URL 的用户名是 Evan,第二个是 Jack,第三个是 John。 When I run the code, my names array would be ["Evan", "Evan", "Evan"] instead of ["Evan", "Jack", "John"] .当我运行代码时,我的名字数组将是["Evan", "Evan", "Evan"]而不是["Evan", "Jack", "John"] I think it is because my code is not waiting for the request result before moving on.我认为这是因为我的代码在继续之前没有等待请求结果。 I am sure there is probably many better ways of doing this, I would appreciate any tips.我相信可能有很多更好的方法可以做到这一点,我会很感激任何提示。

Demo: https://repl.it/@kallefrombosnia/OldfashionedQuintessentialConditions演示: https://repl.it/@kallefrombosnia/OldfashionedQuintessentialConditions

// request package is deprecated so I use this one
const fetch = require('node-fetch');

// random mojang acc's
const urls = [
  'https://api.mojang.com/user/profiles/7125ba8b1c864508b92bb5c042ccfe2b/names',
  'https://api.mojang.com/user/profiles/97f2453a361b44fa9312c496b67f24fc/names',
  'https://api.mojang.com/user/profiles/ac224782efff4296b08cdbde8e47abdb/names',
];


getNames = () => {

  let names = [];
  let round = 0;

  // parse trough urls
  urls.forEach((url, index) => {

    // fetch url info
    fetch(url).then(res =>

      // json convert
      res.json())

      .then(jsonBody => {

        // iteration level
        round++;

        // add name to the array
        names.push(jsonBody[0].name);

        // print some info about user
        console.log(urls[index] + ", Username " + index + ": " + jsonBody[0].name);

        // if this is last round and number of names is same as urls number print all info about users
        if (round === urls.length && names.length === urls.length) {

          console.log("Finished getting Names");
          console.log("UUIDs:" + urls.join(", "));
          console.log("Names:" + names.join(", "));
        }

      })
  })
}

getNames();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当每条记录需要多次 GET 调用时,管理 REST 请求的最佳方法是什么? - What's the best way to manage REST requests when each record requires multiple GET calls? 在 vanilla JavaScript 中向服务器发送 GET 请求的最佳方式是什么? - What is the best way to send a GET request to the server in vanilla JavaScript? STREAM 实时网络摄像头到服务器并返回到 WEB 的最佳方式是什么? - What's the best way to STREAM LIVE WEBCAM to SERVER and BACK TO THE WEB? 服务 API 请求的最佳方式是什么? - What is the best way to serve API requests? 在Electron中,制作ajax请求的最佳方法是什么? - In Electron , what is the best way to make ajax requests? 使用 mongoDB 发送提醒的最佳方式是什么? - What is the best way to send reminders with mongoDB? 通过 HTTP 发送文件的最佳方式是什么? - What is the best way to send files through HTTP? 从nodejs服务器获取数据并将数据发送到生成的角度页面的最佳方法是什么? - what is the best way to get data from nodejs server and send data to angular generated page? 从应用程序服务器向身份验证微服务发送密码的最佳方法是什么? - What is the best way to send password from app server to authentication micro service? 使用express,mysql和node.js的get方法发送变量的最佳方法是什么 - what's the best way to send a variable using method get with express, mysql and node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM