从NodeJS反复调用Rest Endpoint

我对Node家族很新颖。 我有一个服务,有两个端点暴露。 一种是post方法 – 它接收一个有效负载(asynchronous处理),并立即向调用者发送一个确认。 另一个是get方法 – 用于检查先前请求的状态。

例如:

让我们假设两个终点

(1) http:// localhost:8080 / myservice / process / 11

PayLoad - Any JSON Object Response: "Request Received. Under Process" 

(2) http:// localhost:8080 / myservice / checkstatus / 11

 Response: "In-Progress" or "Completed" 

从节点模块,我必须调用第一个端点,然后端点将响应一个确认。 然后一旦收到确认,我需要继续调用第二个GET终点,除非它有一个响应“完成”。

我无法理解如何重复呼叫端点。 任何小代码片段将帮助我理解相同。

提前致谢。

你可以使用setInterval这个:

请注意 :下面是一个伪代码,应该仅供参考。 复制粘贴不会导致工作的解决scheme。

 service.post('url-1', payload).then(function(response){ var timer = setInterval(function(){ service.get('url-2').then(function(resp){ if(resp.data == 'Completed') clearInterval(timer); }); }, 1000); }); 

这将每半秒轮询一次服务,直到获得与endingCondition相匹配的endingCondition (无论您希望如何)。 请注意,本机Promise没有“denodeify”function,虽然它很容易编写自己的或使用第三方库。 done价值并不是绝对必要的,如果需要可以重构,但允许更多的内省。 另外请注意,这是一个devise异味:正如在问题的评论中指出,当asynchronous过程完成时简单地返回服务器的响应会更好。

 let request = require('request'); let promise = require('promise'); let done = false; let poll = promise.denodeify(request.get).bind(request); let p = poll(serviceURL); let handle = setInterval(() => { p.then(res => { if (res === endingCondition) { done = true; clearInterval(handle); } if (!done) { p = poll(serviceURL); } }); }, 500); 

您可以使用SynJS进行这种types的任务,下面是工作代码:

 var SynJS = require('synjs'); var request = require('request'); function requestWrapper(context, url, method, postData) { console.log('get started:', url, method, postData); var result = { done: false }; request({ uri: url, method: method, postData: postData, }, function(error, response, body){ result.done = true; result.body = body; result.response = response; console.log('get finished:', url, method, postData); SynJS.resume(context); // <-indicates that long running functoin is done }); return result; } // synchronous function function myApiSubmitter(modules) { var postRes = modules.requestWrapper(_synjsContext, "https://www.google.com/search","POST","asdasd"); SynJS.wait(postRes.done); // <- waits for SynJS.resume, and checks for postRes.done, continues if postRes.done is true for(var i=0; i<5; i++) { SynJS.wait(500); // <- waits for 500ms var getRes = modules.requestWrapper(_synjsContext, "https://www.google.com/","GET",i); SynJS.wait(getRes.done); // <- waits for SynJS.resume, and checks for getRes.done, continues if getRes.done is true if(getRes.body.indexOf('some_success_message')>=0) // <- some arbitrary condition to break the cycle break; } }; var modules = { SynJS: SynJS, requestWrapper: requestWrapper, }; SynJS.run(myApiSubmitter,null,modules,function () { console.log('done'); }); 

它会产生以下输出:

 get started: https://www.google.com/search POST asdasd get finished: https://www.google.com/search POST asdasd get started: https://www.google.com/ GET 0 get finished: https://www.google.com/ GET 0 get started: https://www.google.com/ GET 1 get finished: https://www.google.com/ GET 1 get started: https://www.google.com/ GET 2 get finished: https://www.google.com/ GET 2 get started: https://www.google.com/ GET 3 get finished: https://www.google.com/ GET 3 get started: https://www.google.com/ GET 4 get finished: https://www.google.com/ GET 4 done