简体   繁体   English

在node.js API中,如何执行必须同步的任务而不冻结浏览器?

[英]In a node.js API how do I perform tasks that must be synchronous without freezing the browser?

To start off this is what I am trying to accomplish: 首先,这是我要完成的工作:

I am trying to do file copies to an array of servers. 我正在尝试将文件复制到服务器阵列。 There are several steps that must be completed in a specific order before and after these copies (for example, stopping IIS, backing up and clearing folders, running a bat file, etc) so they are not single operations. 在这些副本之前和之后,必须按照特定的顺序完成几个步骤(例如,停止IIS,备份和清除文件夹,运行bat文件等),因此它们不是单个操作。

To make this super easy I wrote an API in node.js that does simple tasks like copy files and folders, delete folders, etc. I then wrote a frontend in node.js using an express generator and Pug that uses javascript XMLHhttpRequests to send commands to the API depending on what I needed to do. 为了使这个超级简单,我在node.js中编写了一个API,该API可以完成简单的任务,例如复制文件和文件夹,删除文件夹等。然后,我使用express生成器和Pug在node.js中编写了前端,该Pug使用javascript XMLHhttpRequests发送命令API,具体取决于我需要做什么。 I have the API written and running as well as the frontend. 我已经编写并运行了API以及前端。 Now on to the problems: 现在来解决问题:

If I have my XMLHttpRequest run in synchronous mode (example: xhttp.open("POST", url , false);) when the command is sent to the API to copy a folder if the folder takes several minutes to copy the browser freezes. 如果我的XMLHttpRequest以同步模式运行(例如:xhttp.open(“ POST”,url,false);),则当该命令发送到API以复制文件夹时,如果该文件夹需要几分钟才能复制,则浏览器将冻结。 Chrome displays a "Page Frozen" error. Chrome显示“页面冻结”错误。 However, the job gets done correctly. 但是,这项工作正确完成了。

If I have my XMLHttpRequest run in asynchronous mode (example: xhttp.open("POST", url , true);) then every command gets sent to the API at once so that the fastest operation completes first and the commands are out of order. 如果我的XMLHttpRequest以异步模式运行(例如:xhttp.open(“ POST”,url,true);),那么每个命令都会立即发送到API,以便最快的操作首先完成,并且这些命令不正确。 The copy will fail. 复制将失败。

I've tried searching for a way to make it so that each operation sent from the frontend javascript has to return a SUCCESS (or 200 response) from the API before moving on to the next command but so far all I've seen is "just use synchronous". 我已经尝试寻找一种实现方法,以便从前端javascript发送的每个操作在继续执行下一个命令之前都必须从API返回成功(或200个响应),但到目前为止,我所看到的只是“只需使用同步”。 Right now that's what I'm doing. 现在这就是我正在做的。 That doesn't seem like the best solution even though it works. 即使可行,这似乎也不是最佳解决方案。 Is there a better way to do this in a way that won't freeze the browser? 有没有一种更好的方法可以避免冻结浏览器?

I figured this out by writing a function to handle the requests, setting a counter (for the steps of the process), and putting a switch statement in the if statement for the result. 我通过编写一个处理请求的函数,设置一个计数器(用于处理步骤)以及在结果的if语句中放置一个switch语句来解决这个问题。 It wasn't exactly what I needed but the basics of my solution is in the answers to this question: How can I call ajax synchronously without my web page freezing 这不是我真正需要的,但解决方案的基础在于该问题的答案: 如何在不冻结网页的情况下同步调用ajax

Here's what I did in case it helps anyone else who finds this question: 这是我为防止其他人发现此问题而做的事情:

function myFunction (step, params, url) {
   var xhttp = new XMLHttpRequest();
   xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
         switch(step) {
            case 2:
               //url and params are set here, and step 2 is done here
               myFunction(step, params, url);
               break;
            case 3:
               // and so on and so forth
         }
       }
       xhttp.open("POST", url , true);
       xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       xhttp.send(params);
       step++;
}

//kick off the function
var step = 1;
var url = "my URL to the API with call";
var parameters = "my parameters";
myFunction(step, parameters, url);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM