从NodeJS反复调用Rest Endpoint

时间:2017-01-10 18:22:11

标签: javascript node.js npm node-modules npm-request

我是Node系列的新手。我有一个服务,其中有两个端点暴露。一种是post方法 - 它接收有效负载(异步处理)并立即向呼叫者发送确认。另一个是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终点,除非它有响应"已完成"。

我无法理解如何重复调用端点。 任何小的代码片段都会帮助我理解它。

提前致谢。

3 个答案:

答案 0 :(得分:1)

您可以使用setInterval

请注意:以下是伪代码,仅供参考。复制粘贴不会产生有效的解决方案。

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);
});

答案 1 :(得分:0)

这将每半秒轮询一次服务,直到它获得与time_start匹配的响应(无论你想要的是什么)。请注意,本机Promise没有'denodeify'功能,尽管它很容易编写自己的或使用第三方库。 endingCondition值不是绝对必要的,如果需要可以重构,但允许更多的内省。还要注意,这是一种设计气味:正如在问题的评论中所指出的,当异步过程完成时,最好从服务器返回响应。

done

答案 2 :(得分:0)

您可以使用SynJS执行此类任务,以下是工作代码:

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